Passing an array into hidden_field ROR Passing an array into hidden_field ROR arrays arrays

Passing an array into hidden_field ROR


I would use this technique.

<% @user.roles.each do |role| %>    <%= f.hidden_field :role_ids, :multiple => true, :value => role.id %><% end %>

:multiple adds both the [] to the end of the name attribute and multiple="multiple" to the input element, which is ideal. Rails uses this internally when it generates inputs for arrays, such as in fields_for.

Unfortunately it is not well-documented. I'm going to look into fixing this.


The only thing that works for me (Rails 3.1) is using hidden_field_tag:

<% @users.roles.each do |role| %>    <%= hidden_field_tag "user_role_ids[]", role.id %><% end %> 


Try:

 <% @user.roles.each_with_index do |role| %>    <%= f.hidden_field "role_ids[]", :value => role.id %> <% end %>