newto2rails
newto2rails

Reputation: 53

Setting value to an array in hidden_field of simple form

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?

Upvotes: 3

Views: 3972

Answers (1)

Athar
Athar

Reputation: 3268

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]

Upvotes: 1

Related Questions