hassansin
hassansin

Reputation: 17498

Rails param hash contain array of array for multiple select

My form has a multiple select element like this:

<select id="startup_markets" multiple="multiple" name="startup[markets][]" style="width:100%">
  <option value="fashion">Fashion</option>
  <option value="startups">Startups</option>
  <option value="apps">Apps</option>
  <option value="social-media">social media</option>
  <option value="email-marketing">Email Marketing</option>
</select>

After submitting the post body looks like this:

------WebKitFormBoundaryiICoZLa9BoF6eFMx Content-Disposition: form-data; name="startup[markets][]"

fashion ------WebKitFormBoundaryiICoZLa9BoF6eFMx Content-Disposition: form-data; name="startup[markets][]"

startups

But on rails I get the markets as an array of array:

(byebug) params["startup"]["markets"]
[["fashion", "startups"]]

Wasn't it supposed to be just ["fashion", "startups"]? I'm probably doing something silly here but can't figure out what's wrong. Thanks for the help.

Upvotes: 0

Views: 466

Answers (1)

Nithin
Nithin

Reputation: 3699

The select name probably should be name="startup[markets]"

Also on the ruby side you can handle by calling flatten method on Array.

params["startup"]["markets"].flatten

will result in

=> ["fashion", "startups"]

Upvotes: 2

Related Questions