Module

FormOptionsHelper

Includes:

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 selected="selected" value="2">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>
Constants
COUNTRIES All the countries included in the country_options output.
Public Methods
collection_select Returns <select></select> and <option></option> tags for the collection of existing return values of method for object’s class. The value returned from calling method on the instance object will be selected. If calling method returns nil, no selection is made without including :prompt or :include_blank in the options hash.
country_options_for_select Returns a string of option tags for pretty much any country in the world. Supply a country name as selected to have it marked as the selected option tag. You can also supply an array of countries as priority_countries, so that they will be listed above the rest of the (long) list.
country_select Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.
option_groups_from_collection_for_select Returns a string of <option></option> tags, like #options_from_collection_for_select, but groups them by <optgroup></optgroup> tags based on the object relationships of the arguments.
options_for_select Accepts a container (hash, array, enumerable, your type) and returns a string of option tags. Given a container where the elements respond to first and last (such as a two-element array), the "lasts" serve as option values and the "firsts" as option text. Hashes are turned into this form automatically, so the keys become "firsts" and values become lasts. If selected is specified, the matching "last" or element will get the selected option-tag. Selected may also be an array of values to be selected when using a multiple select.
options_from_collection_for_select Returns a string of option tags that have been compiled by iterating over the collection and assigning the the result of a call to the value_method as the option value and the text_method as the option text. If selected is specified, the element returning a match on value_method will get the selected option tag.
select Create a select tag and a series of contained option tags for the provided object and method. The option currently held by the object will be selected, provided that the object is available. See options_for_select for the required format of the choices parameter.
time_zone_options_for_select Returns a string of option tags for pretty much any time zone in the world. Supply a TimeZone name as selected to have it marked as the selected option tag. You can also supply an array of TimeZone objects as priority_zones, so that they will be listed above the rest of the (long) list. (You can use TimeZone.us_zones as a convenience for obtaining a list of the US time zones.)
time_zone_select Return select and option tags for the given object and method, using #time_zone_options_for_select to generate the list of option tags.
Private Methods
option_text_and_value
option_value_selected?
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.