Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9116995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:48:18+00:00 2026-06-17T04:48:18+00:00

I am a newbie. I just want to create a method in the controller

  • 0

I am a newbie. I just want to create a method in the controller for the following code to avoid repeating again in the view. How can I write in a short and elegant way in ruby on rails?

<% @A = ....... %>
<% @B = ....... %>
<% @C = ....... %>
<% @D = ....... %>
<% @E = ....... %>

<% if (@A || @B || @C) %>
  <label> One: </label>
  <%= ... %>

  <label> Two: </label>
  <%= ... %>
<% end %>

<% if (@A || @B || @C || (@D && @E)) %>
  <label> Three: </label>
  <%= ... %>
<% end %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:48:19+00:00Added an answer on June 17, 2026 at 4:48 am

    assuming your model is called User and the code you provided is placed in “app/views/users/show.html.erb”

    I suggest the following refactoring:
    (you need to replace A,B,C,D and E with the appropriate logic and rename the methods to something more meaningful)

    # in "app/helpers/users_helper.rb"
    module UsersHelper
      def isABC
        return A || B || C
      end
    
      def isDandE
        return D && E
      end
    end
    
    # in "app/views/users/show.html.erb
    <% if (isABC) %>
      <label> One: </label>
      <%= ... %>
    
      <label> Two: </label>
      <%= ... %>
    <% end %>
    
    <% if (isABC || isDandE) %>
      <label> Three: </label>
      <%= ... %>
    <% end %>
    

    You can read up on Rails helpers here: http://paulsturgess.co.uk/articles/49-using-helper-methods-in-ruby-on-rails

    Basically the reponsibility of a controller is to accept incoming web requests, fetch the neccessary data (Model) and call the appropriate View for rendering.

    Rails has Helpers that can be called from your Views. The idiom is that you put any advanced logic used in the view into the Helper

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just a newbie to jQuery can some one help me? I want
This is a newbie type question. I want to create a public method that
I am a newbie to python. I just want to know how i can
I'm completely newbie to Qt i want to create a 800X600 window that just
I'm just a newbie to ruby. I've seen a string method (String).hash . For
Just a quick newbie question here. I have a method that calculates a value
This is a newbie question... I want to create a service which responds to
Being quite a newbie, I've just had my first question answered ( Delphi: TImage.Create
I'm a PHP-Newbie and want to create a simple Website to manage my CD
I'm newbie in vb.net and I just have something to ask you. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.