Kristiyan Tsvetanov Kristiyan Tsvetanov - 7 months ago 28
jQuery Question

Rails, multiple values in an input

How to send an array with the ids of selected elements from my view?
I have a hidden input

= f.hidden_field :selected_items

I can do this when the item is only one with jquery. When clicking on it I take its id and put it in the hidden input's value. However, I do not know how to process clicking on items with id 1,4,7 and 9 and then send it to my controller as :selected_items = [1,4,7,9] for example. Thank you!


You have to explicitly mention that the hidden-field is taking multiple values, you can do this in multiple ways,

You should declare the hidden-field as an array.

 f.hidden_field "selected_items[]"

 f.hidden_field :selected_items, :multiple => true

Append every select box selected value, into the hiddenfield using jquery,

$('#selected_items').val($('#selected_items').val() +','+ selected_item);

Send the values comma-separated and the params will send an array from the hiddenfield.