Reputation: 620
I have a nested form where there are two possible select menus for the same value. At the moment only one of the location_ids is present in the params if both select menus are present.
So even if I select a value in @common_locations_array
and leave @possible_locations
blank, the parameters do not contain the relevant id. However if I select something from @possible_locations
and leave @common_locations_array
blank the value is in the params. I guess its to do with the ordering.
=f.simple_fields_for @job.job_locations.build do |p|
=p.input :location_id, collection: @common_locations_array, label: 'Popular Locations'
=p.input :location_id, collection: @possible_locations, label: 'Clients Locations'
So what would be a way around this? Would I specify my own custom parameter for each one of select menus? if so how would you do that?
Or should I merge the two arrays with some kind of divider in the select menu two seperate the two categories? How would I do that?
I'm not sure.
Thanks
Upvotes: 0
Views: 73
Reputation: 1568
It has to do with the name
attribute of the input
tag that is getting generated. As you have specified the same name to both your input fields, the name
that might be getting generated would be same, i.e., job_locations[location_id]
.
Now, as HTML is executed top-to-bottom, the ones written below will eventually get converted to your prams hash. So, your params[:job_locations][:location_id]
will always have the value that is selected in the second drop-down. To avoid this, you can give different names to the two drop-downs.
like
=p.input :popular_location_id, collection: @common_locations_array, label: 'Popular Locations'
=p.input :client_location_id, collection: @possible_locations, label: 'Clients Locations'
Now, both the params can be accessed via: params[:job_location][:popular_location_id]
and params[:job_location][:client_location_id]
Upvotes: 1