Flowdock
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) public

Return select and option tags for the given object and method using options_from_collection_for_select to generate the list of option tags.

Show source
Register or log in to add new notes.
August 26, 2008
7 thanks

Customizing prompt

The :prompt option not only accepts a boolean value. It can also be given a string to define another than the standard prompt ‘Please select’. Referring to the example it could read:

collection_select(:post, :author_id, Author.find(:all),
                  :id, :name_with_initial,
                 {:prompt => 'Please select the author of this post'})
October 10, 2008
2 thanks

Make an action onchange

collection_select(nil, :provincia_id, @provincias, :id, :nombre, {:prompt => “Seleccione una provincia”}, {:onchange => “#{remote_function(:url => {:controller => ‘direccions’, :action => ”update_pueblos“}, :with => ”‘provincia_id=’+value“)}”})

March 17, 2009
0 thanks

Post =form post, NOT the http POST method

Post =forum post, NOT the http POST method

Note: any “Post” on this page has nothing to do with http methods. When I just looked at the collection_select code I was thrown off.

June 29, 2009
0 thanks

:selected

If you want some object to be selected by default, be sure to use its id, not the whole object.

 collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:selected => current_book.authors.map(&:id)})
#=> :selected => [1,2,3,4]

and not

collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:selected => current_book.authors})