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

  • SEARCH
  • Home
  • 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 6635391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:02:53+00:00 2026-05-25T23:02:53+00:00

I have such code, for making chain selects in my form View for index

  • 0

I have such code, for making chain selects in my form
View for index action:

<%= form_tag do %>
    <%= collection_select(*@brands_select_params) %>
    <%= collection_select(*@car_models_select_params) %>
    <%= collection_select(*@production_years_select_params) %>
    <% # Пока еще никто ничего не выбрал %>
<%= submit_tag "Send", :id => "submit", :name => "submit" %>

And my controller:

class SearchController < ApplicationController
  def index
    @brands = Brand.all
    @car_models = CarModel.all

    if (params[:brand].blank?)
      @brands_select_params = [:brand, :id, @brands, :id, :name, :prompt => "Выбирай брэнд"]

      if params[:car_model].blank?
        @car_models_select_params = [:car_model, :id, @car_models, :id, :name, { :prompt => "Model" }, \
                                   { :disabled => "disabled" }]
        @production_years_select_params = [:production_year, :id, @car_models, :id, :name, { :prompt => "Year" }, \
                                         { :disabled => "disabled" }]
      end
    else
      @brands_select_params = [:brand, :id, @brands, :id, :name, { :selected => params[:brand][:id] } ]
      if params[:car_model].blank?
        @car_models_select_params = [:car_model, :id, Brand.find(params[:brand][:id]).car_models, :id, :name, \
                                   { :prompt => "And model now" } ]
        @production_years_select_params = [:production_year, :id, @car_models, :id, :name, { :prompt => "Year" }, \
                                         { :disabled => "disabled" } ]
      else
        @car_models_select_params = [:car_model, :id, Brand.find(params[:brand][:id]).car_models, :id, :name, \
                                   { :selected => params[:car_model][:id] } ] unless params[:car_model][:id].empty?
        @production_years_select_params = [:production_year, :id, CarModel.find(params[:car_model][:id]).production_years, :id, :year, \
                                         { :prompt => "And year now" } ] unless params[:car_model][:id].empty?
      end
    end
  end
end

As you can see, too many ifs in my controller code. And i gonna add more conditions there. After that anyone who read that code will get brain corruption. So i just wanna make it in real Ruby way, but don’t know how. Please, help, guys. How should i refactor this bunch of crap?

  • 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-05-25T23:02:54+00:00Added an answer on May 25, 2026 at 11:02 pm

    I think a big part of the problem is you’re doing too much in your controller. Generating markup (and IMO that includes building parameter lists for form helpers) should be done in views and view helpers. So:

    module SearchHelper
      def brand_select brands, options={}
        collection_select :brand, :id, brands, :id, :name, :options
      end
    
      def car_model_select car_models, options={}
        collection_select :car_model, :id, car_models, :id, :name, options
      end
    
      def production_year_select years, options={}
        collection_select :production_year, :id, years, :id, :year, options
      end
    end
    

    Then you can cut your controller down to this:

    def index
      @brands     = Brand.all
      @car_models = CarModel.all
    
      @selected_brand_id      = params[:brand]      && params[:brand][:id]
      @selected_car_model_id  = params[:car_model]  && params[:car_model][:id]
    
      @production_years = @selected_car_model_id ?
        [] : CarModel.find(@selected_car_model_id).production_years
    end
    

    And in your view:

    <%= brand_select @brands, :prompt   => "Выбирай брэнд",
                              :selected => @selected_brand_id
    %>
    <%= car_model_select @car_models, :prompt   => "Model",
                                      :selected => @selected_car_model_id
    %>
    <%= production_year_select @production_years, :prompt   => "Year",
                                                  :selected => @selected_car_id
    %>
    

    I suspect you could simplify this even more using form_for and fields_for and get rid of the helpers entirely, but it depends a bit on how your model associations are set up.

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

Sidebar

Related Questions

I have such code in my JSF template: <h:form> <table id=users cellspacing=0> <a4j:repeat var=person
I have such code: function allValid() { $('input').each(function(index) { if(something) { return false; }
We have a place in a code of such form: void processParam(Object param) {
I have such code: <h:inputText id=input value=#{bean.input}> <f:convertNumber /> <rich:ajaxValidator event=onblur /> </h:inputText> I
i have such code var prj = _dataContext.Project.FirstOrDefault(p => p.isPopular == true); if (prj
For reference, the code is for the motorola 68008. Say I have code such
So I have some openGL code (such code for example) /* FUNCTION: YCamera ::
I have such a basic problem in Delphi,I can't solve it. My Code: Note:DataR
I was wondering, if I have some code such as: $result = $db->query($sql); //
I have been looking into coloring objects like ellipses with code such as SolidBrush

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.