I’m implementing a form based on Ryan Bate’s [always excellent] Railscast, #52, Update Through Checkboxes, http://railscasts.com/episodes/52-update-through-checkboxes
In my case, I’m actually wanting to pass through an array of ids for each checkbox selected.
The key line in my form looks like this,
<td"><%= check_box_tag "sales_ids[]", batch["sales_ids"] %></td>
batch[“sales_ids] itself is an array of ids, and the outcome I’m wanting is for an array of all the checked items (each representing an array of ids) to be passed along.
If I inspect the individual batch[“sales_ids”] from the form, then I can confirm it is indeed an array, for example, [3907, 3908, 3909] (remember, I want to pass along an array of these items for each row checked)
The key line from the method that handles the form looks like this,
params[:sales_ids].each { |batch| Sales.update_all(["code=?", 99], :id => batch) }
The intent of this code, again based on the Railscast example, is to iterate over the array of arrays, and update each sub-array of ids.
When I examine the received array, I see, [“5412”, “3907 3908 3909”, “2452”]
The sub-array isn’t handled as an array and (for reasons I don’t understand) only the first item each ‘substring’ (in this case “5412”, “3907”, “2452”) is updated.
It seems that the passed subarray isn’t actually an array, or something else is awry.
I’d appreciate help in how to pass and receive these ids as an array of arrays. Thanks.
as far as I know, passing arrays of arrays in this way won’t work via a form. However there is a work around that should work fine for you.
in the view do this for your checkboxes: (I’m assuming batch[“sales_ids”] is an array)
And in your controller do this:
Instead of passing an array of arrays, we are now passing an array of comma separated values. In the controller these comma separated values are turned into an array.