Form Select Helper in Ruby on Rails

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.

cialis 20mg price

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.

  1. <select name="gender">
  2. <option value="1">Male</option>
  3. <option value="2">Female</option>
  4. </select>

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

  1. <%= 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.

  1. <%= 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.

  1. <%= select_tag "payment", options_for_select([ "Male", "Female" ], "Male") %>

or

  1. <%= select_tag "payment", options_for_select(%w{ Male Female }) %>

You can also do multi-select boxes:

  1. <%= select_tag 'payment[]', options_for_select(@genders),
  2. :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.

  1. <%= collection_select(:gender, :id, @genders, :id, :name,
  2. 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.