Multi-select list boxes in Ruby On Rails
Multi-select list boxes in Ruby On Rails 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.
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 ...
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.
Created at: Fri Mar 31 18:54:00 UTC 2006 Updated at: Wed Jan 16 12:16:12 UTC 2008

