1

My question is quite simple, I have a plain HTML form (no helpers, no models) and I want to send the parameters so params dictionary appears to be an array.

This is my code:

<div class="headers_line">
  <div class="header_field">
    <input type="text" placeholder="Header name" name="header[0][name]" class=
    "parameter_input" />
  </div>

  <div class="header_field">
    <input type="text" placeholder="Value" name="header[0][value]" class=
    "parameter_input" />
  </div>
</div>

<div class="headers_line">
  <div class="header_field">
    <input type="text" placeholder="Header name" name="header[1][name]" class=
    "parameter_input" />
  </div>

  <div class="header_field">
    <input type="text" placeholder="Value" name="header[1][value]" class=
    "parameter_input" />
  </div>
</div>

<div class="headers_line">
  <div class="header_field">
    <input type="text" placeholder="Header name" name="header[2][name]" class=
    "parameter_input" />
  </div>

  <div class="header_field">
    <input type="text" placeholder="Value" name="header[2][value]" class=
    "parameter_input" />
  </div>
</div>

And this is what my params looks like:

"header"=>{"0"=>{"name"=>"jnjnnj", "value"=>"nnjjn"}, "1"=>{"name"=>"jnnjjn", "value"=>"jnjnnj"}, "2"=>{"name"=>"jnjnjnjn", "value"=>"jnnj"}, "3"=>{"name"=>"", "value"=>""}}

Trying to use header[][name] and header[][value] didn´t work either.

Thanks

3
  • Almost, it generates an array of names and another of values, but it's better: "header"=>{"name"=>["ijijjijijij", "ijadsijdasij", "adsadjsiiadjsjidsa", "jidadisjajdisai", ""], "value"=>["ijsijdsaijadsij", "ijdasjidasijij", "jadisidjsajidsaji", "sajidsji", ""]} Commented Nov 13, 2013 at 21:41
  • are you trying to flatten the hash to get rid of the "0","1" keys etc? is that what you want to achieve? Commented Nov 13, 2013 at 21:41
  • I want to achieve: params[:header] to be an array of {name: '2323', value: '123'} Commented Nov 13, 2013 at 21:49

2 Answers 2

1

Sorry, I was totally confused in the previous answer.

params[:header].values.map(&:symbolize_keys) will give you the array you want.

You cannot get it directly.

Sign up to request clarification or add additional context in comments.

Comments

1

Ok, based on @house9 comment I did the following:

I used header[name][] and header[value][]

Then I created an array of hashes by doing:

headers = params[:header][:name].zip(params[:header][:value])

Comments

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.