3

I want something like this:

<%= f.hidden_field :ids, :multiple => true, :value => array %>

but it's not saving anything. Am I doing anything wrong or is there another way to approach this?

1

1 Answer 1

1

try this

<% array.each do |a| %>
  <%= f.hidden_field :ids, :multiple => true, :value => a %>
<% end %>

some says this wont work in rails 4

 <%= f.hidden_field :ids, :multiple => true, :value => a %>

replace that with this incase it wont work for you

 <%= f.hidden_field "ids[]", value:  a %>

or you can try using hidden_field_tag as well.. if you can access without object

<% array.each do |a| %>
  <%= hidden_field_tag "ids[]", a %>
<% end %>

and in controller you can access using this params[:ids]

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

9 Comments

I tried this exactly and it does not seem to be working. After submitting the form, :ids has a null value.
try this <%= f.hidden_field "ids[]", value: a %> inside loop. im hoping it will work.
i just tested this <% (0..10).each do |a| %> <%= f.hidden_field :ids, :multiple => true, :value => a %> <% end %> in my project with user model and i fetch from params like this params[:user][:ids] im using rails 4.2.0
im going to try this now <%= f.hidden_field "ids[]", value: a %>
added option hidden_field_tag as well and you can access using params[:ids] in controller
|

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.