Well, a quick post to cover a very simple (but quickly frustrating) topic. I actually ran across this in my day job while trying to access a multiple select set of values in PHP.
I discovered that the multiple select tag had to have [] appended to its name before PHP would recognize it was an array of values. So the select field with name="myoptions" would now have to be called name="myoptions[]" to allow proper access to all the values that are submitted (otherwise you will only get the first value that is submitted).
I was curious after work and decided to check if the same rule applied to Rails. To my surprise it did. Thus a basic multiple select box coded like so:
<%= start_form_tag( {:action => "index"}, {:id => "select"}) %>
Multiple Select
<%= select_tag "myoptions[]", options_for_select(["1","2","3","4"]), :multiple => true %>
<%= submit_tag %>
<%= end_form_tag %>
(Pay special attention to the fact that the name is "myoptions[]")
Produces a form that looks like so:
And since you can Ctrl or Shift click multiple options - you can easily access these options via :params in Rails by something simple such as:
params[:myoptions][0] #the first element - blank if it was not selected, otherwise the value 1
params[:myoptions][1] #the second element - blank if it was not selected, otherwise the value 2
params[:myoptions][2] #the third element - blank... etc.
Well, that was simple, but alas it does not cover everything. If you want to learn more about this - for the lack of my time to research other good sources - check out the rails wiki that has some info. I look forward to posting again soon, thanks for stopping in.
