I am looking to create 3 separate collection_selects to choose players for a team, where Team has_many players. However, if I have the following code 3 times…
= f.collection_select :player_ids, @season.players, :id, :name
… it only sends one back in the params, since they all point to the same thing.
How can I use three collection selects to add three has_many values?
I do not want to use
= f.collection_select :player_ids, @season.players, :id, :name, {}, { :multiple => true }
You’re getting only one in the params, because all three dropdowns share the same html
<select>element ID and name.If you don’t like the multiple approach, the only thing I can think of is specifying manually the dropdown IDs and names as follow:
The IDs can be whatever you prefer but all three must be different. The real important thing is the “name” attribute, as it must match the model you’re updating (in this case I suppose it’s season) and having the final brackets [] will indicate it’s an array of IDs (one ID per-dropdown)