Curious what the 'rails way' of handling the situation when a user checks multiple checkboxes (with the same name value), and it gets posted back to the controller.
How would you check if multiple items were selected, then splitted on the ID values etc.
The easiest way of doing this is to set those checkboxes up to become an array.
HTML:
<input type="checkbox" name="tag_ids[]" value="1" /> <input type="checkbox" name="tag_ids[]" value="2" /> <input type="checkbox" name="tag_ids[]" value="3" />
Controller:
tag_ids = params[:tag_ids]
(Of course, you'd probably be using form_for
-based helpers in the view, and therefore mass-assigning the tag IDs. This is just the most generic example.)
f.check_box :tag_ids, {multiple: true}, 1, nil
Is the right answer:
Here is the reason, there is a 'multiple: true' option that allows your input to be placed in an array. If there isn't a multiple: true option this will not be allowed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With