1

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!

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

How to put the values in? If I do $("#selected_items").val(selected_item); wouldn't I loose the ids which are already in :selected_fields?
You can Just append the values to the hidden-field one after the other. Like. $('#selected_items').val($('#selected_items').val() +','+ selected_item);
Send the comma separated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.