0

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.

1
  • Sorry guys, I'm working on someone else's project and didn't notice there is a filter setup that manipulates the params hash before reaching controller method. Wasted hours and found the problem within mintues after submitting to SO :@ Commented Jun 22, 2015 at 9:35

1 Answer 1

2

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

1 Comment

Turns out it was a filter that modified the params hash. Thanks for the flatten suggestion though.

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.