2

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 :(

1
  • What you were missing is that you misread my answer. You are using the _tag sufix, and that doesn't allow you to pass the multiple option. Commented Jan 25, 2015 at 18:28

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

see my edit: I'm actually getting "employee_ids" => "{:multiple=>true, :value=>170}",
That's not the problem. The problem is that you are using the _tag suffix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.