Reputation: 1182
I have a form in which people can select a date, and click "filter". This date is then passed back to the controller as a parameter, and the view is reloaded with a list of users named @users
I then have a form that I want to pass the @users back as an array
I tried this
= form_tag('/complete_export', :method => "put", :id => 'export') do |f|
%p=hidden_field_tag "users", "#{@users.map(&:id)}"
%p=submit_tag("Export results »")
But when it gets back to the controller, the parameter arrives back looking like this...
[8] pry(#<AccountsController>)> params[:users]
"[53533, 51780, 17418, 153, 50667, 141, 48968, 48343, 48338, 47377, 44736, 40410, 27878, 172, 150, 34457, 37757, 26715, 31240, 31178, 30016, 184, 177, 196, 193, 155, 170]"
so it's actually coming back as a string. How do I pass it to the controller as an array, or change it in the controller into an array.
EDIT
So I have now amended my form as such...
= form_tag('/complete_export', :method => "get", :id => 'export') do |f|
- @users.each do |user|
=hidden_field_tag :user_ids, :multiple => true, :value => user.id
%p=submit_tag("Export results »")
Back in the controller, the params are showing as...
"utf8" => "✓",
"employee_ids" => "{:multiple=>true, :value=>170}",
"commit" => "Export results »",
"action" => "complete_payrite_export",
"controller" => "accounts"
clearly I'm missing something :(
Upvotes: 2
Views: 2539
Reputation: 15791
This is because you are using hidden_field_tag
with f.hidden_field
arguments, here is hidden_field_tag
alternative:
- @users.each do |user|
= hidden_field_tag 'user_ids[]', user.id
Upvotes: 2
Reputation: 23713
I would use the multiple
option, which in my opinion is cleaner than doing a casting in the controller.
- @users.each do |user|
= f.hidden_field :user_ids, :multiple => true, :value => user.id
You will get an array named user_ids
in your controller.
Upvotes: 2