Ruby on Rails
MultipleSelectOptionsHelper (Version #5)

NOTE: multiple checkboxes with habtm (has_and_belongs_to_many) has been moved to it’s own page: CheckboxHABTM
—————————————————————————————————————————

haHA! Ask for it and I shall receive! (After I implement it, of course). This has been accepted and released in Rails 0.6.5. Enjoy!

I wished for it but I went ahead and did it because I needed it. The diff is at http://junk.elitists.net/~scott/form_options_helper.diff

I wish that in the form options helper that I could pass in an array of values for selected keys to the methods so that when I’m using a multiple select all keys in the array would be selected.

My example scenario:

Database:


frames (has_many :pages) ------------------------ id

pages (belongs_to :frame, has_many :links)
—————————————————————
id
frame_id
title

links (belongs_to :page)
————————————
id
page_id
link_text
link_to (refers to page.id)


And I would like to do something like this:

<pre>

  1. Page Controller
    page = Page.find(params‘id’)
    @frame = @page.frame
    @links = @page.links.collect { |l| l.link_to }

<pre> # View <select multiple> <%= options_from_collection_for_select @frame.pages, "id", "title", @links %>


And this would create a select with options for all of the links known to the frame with the links known to the page selected. Dig it?

Getting Multiple Selections in @params

From the January 17th, 2005 ActionPack CHANGELOG

Added an option for getting multiple values on a
single form name into an array instead of having the
last one overwrite. This is especially useful for groups
of checkboxes, which can now be written as:


<input type="checkbox" name="rights[]" value="CREATE" />
<input type="checkbox" name="rights[]" value="UPDATE" />
<input type="checkbox" name="rights[]" value="DELETE" />

…and retrieved in the controller action with:


@params["rights"] # => [ "CREATE", "UPDATE", "DELETE" ]

The old behavior (where the last one wins, “DELETE” in
the example) is still available. Just don’t add “[]” to the
end of the name. [Scott Baron]

So, in the case of an HTML “select” element, do something like the following to get multiple selections returned as an array in @params:


<select id="org_people" name="org[people][]" size="20" multiple="multiple">
	<%= options_for_select @all_people, @member_people %>
</select>

NOTE: multiple checkboxes with habtm (has_and_belongs_to_many) has been moved to it’s own page: CheckboxHABTM
—————————————————————————————————————————

haHA! Ask for it and I shall receive! (After I implement it, of course). This has been accepted and released in Rails 0.6.5. Enjoy!

I wished for it but I went ahead and did it because I needed it. The diff is at http://junk.elitists.net/~scott/form_options_helper.diff

I wish that in the form options helper that I could pass in an array of values for selected keys to the methods so that when I’m using a multiple select all keys in the array would be selected.

My example scenario:

Database:


frames (has_many :pages) ------------------------ id

pages (belongs_to :frame, has_many :links)
—————————————————————
id
frame_id
title

links (belongs_to :page)
————————————
id
page_id
link_text
link_to (refers to page.id)


And I would like to do something like this:

<pre>

  1. Page Controller
    page = Page.find(params‘id’)
    @frame = @page.frame
    @links = @page.links.collect { |l| l.link_to }

<pre> # View <select multiple> <%= options_from_collection_for_select @frame.pages, "id", "title", @links %>


And this would create a select with options for all of the links known to the frame with the links known to the page selected. Dig it?

Getting Multiple Selections in @params

From the January 17th, 2005 ActionPack CHANGELOG

Added an option for getting multiple values on a
single form name into an array instead of having the
last one overwrite. This is especially useful for groups
of checkboxes, which can now be written as:


<input type="checkbox" name="rights[]" value="CREATE" />
<input type="checkbox" name="rights[]" value="UPDATE" />
<input type="checkbox" name="rights[]" value="DELETE" />

…and retrieved in the controller action with:


@params["rights"] # => [ "CREATE", "UPDATE", "DELETE" ]

The old behavior (where the last one wins, “DELETE” in
the example) is still available. Just don’t add “[]” to the
end of the name. [Scott Baron]

So, in the case of an HTML “select” element, do something like the following to get multiple selections returned as an array in @params:


<select id="org_people" name="org[people][]" size="20" multiple="multiple">
	<%= options_for_select @all_people, @member_people %>
</select>