module FormOptionsHelper

Ruby on Rails 2.2.3

Since v2.2.3

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Provides a number of methods for turning different kinds of containers into a set of option tags.

Options

The collection_select, country_select, select, and time_zone_select methods take an options parameter, a hash.

  • :include_blank - set to true or a prompt string if the first option element of the select element is a blank. Useful if there is not a default value required for the select element.

For example,

select("post", "category", Post::CATEGORIES, {:include_blank => true})

could become:

<select name="post[category]">
  <option></option>
  <option>joke</option>
  <option>poem</option>
</select>

Another common case is a select tag for an belongs_to-associated object.

Example with @post.person_id => 2:

select("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'})

could become:

<select name="post[person_id]">
  <option value="">None</option>
  <option value="1">David</option>
  <option value="2" selected="selected">Sam</option>
  <option value="3">Tobias</option>
</select>
  • :prompt - set to true or a prompt string. When the select element doesn’t have a value yet, this prepends an option with a generic prompt – “Please select” – or the given prompt string.

Example:

select("post", "person_id", Person.find(:all).collect {|p| [ p.name, p.id ] }, {:prompt => 'Select Person'})

could become:

<select name="post[person_id]">
  <option value="">Select Person</option>
  <option value="1">David</option>
  <option value="2">Sam</option>
  <option value="3">Tobias</option>
</select>

Like the other form helpers, select can accept an :index option to manually set the ID used in the resulting output. Unlike other helpers, select expects this option to be in the html_options parameter.

Example:

select("album[]", "genre", %w[rap rock country], {}, { :index => nil })

becomes:

<select name="album[][genre]" id="album__genre">
  <option value="rap">rap</option>
  <option value="rock">rock</option>
  <option value="country">country</option>
</select>

Includes

Methods (defined here)

Private methods

(2) Implementation detail — not part of the public API.

Methods (inherited)

From ERB::Util (3)

Type at least 2 characters to search.

↑↓ navigate · open · esc close