Search code examples
ruby-on-railsselectmergenomethoderror

Rails collection_select not working but equivalent select with options_from_collection_for_select does


Here is my code snippet that works exactly as intended:

<%= f.select(:other_model_id, 
             options_from_collection_for_select(
               OtherModel.all, 
               :id, 
               :full_name,
               { :selected => @this_model.other_model_id} )) %>

But for some reason, this doesn't work:

<%= f.collection_select :this_model, :other_model_id, 
                         OtherModel.all, :id, :full_name %>

There error I get is:

undefined method `merge' for :full_name:Symbol

Any suggestions? The fact that :full_name works properly with the working code leads me to believe I screwed up the syntax in the simplified collection_select code and that the problem is not elsewhere.


Solution

  • I think you're mixing up two different collection_select methods. You're calling the FormBuilder#collection_select using the FormOptionsHelper#collection_select arguments. Maybe you want this:

    <%= f.collection_select :other_model_id, OtherModel.all, :id, :full_name %>
    

    Or perhaps this:

    <%= collection_select :this_model, :other_model_id, OtherModel.all, :id, :full_name %>
    

    You end up trying to put :full_name in the options argument but that's supposed to be a Hash, that's where the complaint about "no merge method" comes from.