Multi-select list boxes in RubyOnRails
Multi-select list boxes in RubyOnRails are pretty simple if you have an example to refer to. Here's a silly attempt to build a UI where the user selects the flavors of ice cream for an ice cream sunday.
<%= select_tag 'flavors[]',
optionsforselect( [['vanilla','1'], ['strawberry','2'], ['chocolate','3']] ),{ :multiple => true, :size =>5 } %>
Some key point here are
- The variable name must end in '[]' to show that it is an array.
- You should use options_for_select to render the list in the proper format.
- Instead of the hardcoded options array that I used in the code above you probably what to use a method on some model class to return the list to display.
To harvest the parameters in your action, do something like this ...
@sunday = IceCreamSunday.create(params[:sunday])
unless has_errors?(@sunday)@sunday.flavors << Flavor.find(params[:flavors].collect{|char| char.to_i})redirectto makethedishurl(:id => @sunday.id)end
Update: Get the list data from Rails ...
I've recently released a plug-in to that can help get the selection list data to the list from your code which will help with hard coded selection lists not with dynamic selection lists.
-- Mark Windholtz
