Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an array through a hidden_field_tag in Rails

I did find this question on SO, but it didn't help, really.

So, I'd like to pass an array through a hidden field tag. As of now my code is:

<%= hidden_field_tag "article_ids", @articles.map(&:id) %>

This obviously does not work since it passes the ids as a string.

How do i do it?

like image 949
Shreyas Avatar asked Dec 22 '10 11:12

Shreyas


2 Answers

Hi maybe there is better solution but you may try

<% @articles.map(&:id).each do |id| %>
  <%= hidden_field_tag "article_ids[]", id %>
<% end %>
like image 178
Bohdan Avatar answered Sep 24 '22 17:09

Bohdan


The following worked for me on Rails 4.1.10

<% @your_array.map().each do |array_element| %>
    <%= hidden_field_tag "your_array[]", array_element %>
<% end %>
like image 34
Conor Avatar answered Sep 22 '22 17:09

Conor