2

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?

newto2rails
  • 43
  • 1
  • 7
  • http://stackoverflow.com/questions/7175663/passing-array-via-hidden-fields-to-rails you need to traverse and add to it . example is this http://stackoverflow.com/questions/2015204/passing-an-array-into-hidden-field-ror – Athar Jul 29 '15 at 15:22

1 Answers1

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]

Athar
  • 3,258
  • 1
  • 11
  • 16
  • I tried this exactly and it does not seem to be working. After submitting the form, :ids has a null value. – newto2rails Jul 29 '15 at 15:28
  • try this `<%= f.hidden_field "ids[]", value: a %>` inside loop. im hoping it will work. – Athar Jul 29 '15 at 15:29
  • 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 – Athar Jul 29 '15 at 15:36
  • im going to try this now `<%= f.hidden_field "ids[]", value: a %>` – Athar Jul 29 '15 at 15:38
  • added option hidden_field_tag as well and you can access using params[:ids] in controller – Athar Jul 29 '15 at 15:41
  • on the console it shows that it indeed saves :ids with the new values, but on the server nothing changes – newto2rails Jul 29 '15 at 15:55
  • Ok, I solved my problem. I needed to add input_html: {:multiple => true}...thank you for the help! – newto2rails Jul 29 '15 at 15:58
  • I've come back to this and the problem wasn't really fixed. The array is overridden every time the loop executes so in the end, the array only contains one value...do you know of a solution? – newto2rails Jul 30 '15 at 16:41
  • what happened. can you please rephrase the problem. i.e is the value didnt get set.? or it is overwritten.? – Athar Jul 31 '15 at 18:00