Ruby on Rails: Submitting an array in a form
If your html form has input fields with empty square brackets, then they will be turned into an array inside params in the controller.
# Eg multiple input fields all with the same name:<input type="textbox" name="course[track_codes][]" ...># will become the Array params["course"]["track_codes"]# with an element for each of the input fields with the same name
Added:
Note that the rails helpers are not setup to do the array trick auto-magically. So you may have to create the name attributes manually. Also, checkboxes have their own issues if using the rails helpers since the checkbox helpers create additional hidden fields to handle the unchecked case.
= simple_form_for @article do |f| = f.input_field :name, multiple: true = f.input_field :name, multiple: true = f.submit
TL;DR version of HTML []
convention:
Array:
<input type="textbox" name="course[track_codes][]", value="a"><input type="textbox" name="course[track_codes][]", value="b"><input type="textbox" name="course[track_codes][]", value="c">
Params received:
{ course: { track_codes: ['a', 'b', 'c'] } }
Hash
<input type="textbox" name="course[track_codes][x]", value="a"><input type="textbox" name="course[track_codes][y]", value="b"><input type="textbox" name="course[track_codes][z]", value="c">
Params received:
{ course: { track_codes: { x: 'a', y: 'b', z: 'c' } }