Rails: getting multiple selected values from selection box

I got stuck for last half an hour at ‘getting the values of multiple selected items in a muliple selection box’ using Ruby/Rails. Got it after hard ‘googling’ and got the solution here.

When trying to get multiple selected values from a <SELECT> tag into @params, call your parameter something with [] on the end. That way rails knows it’s multiple.

So instead of

<select name="job[techs]" size="5" multiple="multiple">

Use:

<select name="job[techs][]" size="5" multiple="multiple">

Thanks google and Ruby/Rails community !

Advertisements

4 thoughts on “Rails: getting multiple selected values from selection box

  1. Thanks for posting this. It fixed me up in 30 seconds!

    You can also use the select_tag like so:

    true%>

    This assumes that you have “has_many :techs” in your Job class.

  2. ack! The thing ate all my text since it contained brackets… dang…

    select_tag “job[tech_ids][]”, options_from_collection_for_select(@all_techs, “id”, “name”), :multiple=>true

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s