It’s crazy how little documentation there is on using selects (drop down lists) inside form-helpers in Rails. So, it can get quite confusing when it comes to using the different select helpers; select, select_tag and collection_select.
In order to address this inadequate documentation and examples, I thought I would post here, some of the differnces between them with some examples so you can choose for yourself, the best one to use.
The selection box has some key parts, the name, which is required, and used by the browser when submitting the <select> choices to the server. The option tags, each of which are made up from a “value” and “text” pair, the “value” to identify the select item in the server, and the “text” which will be displayed to the screen.
There are three different select form helpers in ruby on rails, “Select”, “select_tag” and “collection_select”. Let’s compare them.
select(object, method, choices, options = {}, html_options = {}) Defined in ActionView::Helpers::FormOptionsHelper
<%= select( "payment", "id", { "Male" => "1", "Female" => "2"}) %>
Select can be used in conjunction with a model object as seen in this example, an instance variable is passed into choices, but is being converted into an array of arrays.
<%= f.select :gender_id, @genders.map {|r| [r.name,r.id] } %>
Use select_tag when you require a drop-down selection box populated with data not sourced from a database, and are happy to hard code the default selected option tag. Select_tag should also be used when you want to process your form as a GET, rather than a POST.
<%= select_tag "payment", options_for_select([ "Male", "Female" ], "Male") %>
or
<%= select_tag "payment", options_for_select(%w{ Male Female }) %>
You can also do multi-select boxes:
<%= select_tag 'payment[]', options_for_select(@genders),
:multiple => true, :size => 3 %>
(where the controller defines @genders as a hash of values).
In this example the gender methods are being added to the select box via a model object, notice how we use the option, :prompt to add an additional option tag to the select box, which will be selected by default. Note that if @object.method matches one of the option tags, this will be selected by default, and :prompt wont appear in the list.
<%= collection_select(:gender, :id, @genders, :id, :name,
options ={:prompt => "-Select a gender"}, :class =>"gender") %>
In sum:
Use select when you require a basic drop-down selection box populated with data not sourced from a database.
- The object is the name of an instance variable. This is typically a model object (singular name of the table whose data your displaying, or in other words, the table record).
- The method is the attribute of that instance variable. This is typically a field/column of the table whose data your displaying (really an ActiveRecord method).
- Together the object and method specify the name of the select statement in the generated html choices can be any enumerable object e.g arrays and hashes and results of database queries, and contains the option tags for the select box.
- The optional options argument takes various “options” some of which are listed below in the examples.
- The optional html_options argument allows css to be used for styling the select box.
- If one of the option tags in choices matches @object.method, that option tag will be selected.
Use collection_select when you require a drop-down selection box, whose source is a model/object
- The object is the singular name of the table whose data your displaying (the table record).
- The method is the field/column of the the relevant data (really an ActiveRecord method).
- Together the object and method specify the name of the select statement in the generated html collection takes the option tags for the select box, this can be a hash or array.
- The value_method is the field/column to use for the value of the option tags in your html.
- The text_method is the field/column to use for the visible text of the option tags in your html.
- The optional options argument takes various “options” some of which are listed below in the examples.
- The optional html_options argument allows css to be used for styling the select box.
- If one of the option tags in collection matches @object.method, that option tag will be selected.
Some of the content for this post came from here – check it out for a more detailed explaination of the differences between the 3 select form helpers.
nice post! I was seaching quite a while for this answer. thks!