Creating Selection Lists

Selections lists are, in many ways, like radio buttons on a larger scale. Rather than filling a screen with radio buttons, a list lets you hide the options except during that critical time when you’re actually making a selection. Showing radio buttons for over 190 countries would take up a huge amount of screen real estate. Selection lists offer a much more compact but still convenient way for users to make choices.

Rails has a number of helper methods for creating selection lists, but the simplest place to start is the select method. In its most basic form, select takes two arguments: the attribute that populates it and a set of choices. Choices can be represented in a number of different ways, from a simple array of strings to a hash or other more complex set of values.

Note

At the time this was written, this simplest form of select wasn’t actually documented in the Rails API docs. If you look at the documentation for a function and it seems like it’s more complex than you need, it’s sometimes worth experimenting to see whether a simpler form will work. The docs often seem to give priority to more complex use cases. (As you become a guru, you’ll likely be able to look at the Rails source code and figure it out, but Ruby’s many options make it tricky at first.)

Using an array of strings, the call to create a selection list might look like:

<p>
   <b>Country</b><br />
   <%= f.select (:country, ['Canada', 'Mexico', 'United Kingdom', 'United States of
America'])%>
</p>

This generates:

<p>
    <b>Country</b><br />
    <select id="person_country" name="person[country]"><option
value="Canada">Canada</option>
<option value="Mexico">Mexico</option>

<option value="United Kingdom">United Kingdom</option>
<option value="United States of America">United States of America</option></select>
  </p>

When working from a simple list of strings, Rails creates option elements whose value attributes—the values sent back to the server—are the same as the content displayed to the user, as shown in Figure 6-4.

A selection list created from an array of strings

Figure 6-4. A selection list created from an array of strings

You can be a little more specific about what you want by using a two-dimensional array. The display values come first, and the values that go to the server come second:

<p>
   <b>Country</b><br />
   <%= f.select (:country, [ ['Canada', 'Canada'],
                             ['Mexico', 'Mexico'],
                             ['United Kingdom', 'UK'],
                             ['United States of America', 'USA'] ])%>
</p>

This still looks the same as the result in Figure 6-4, but the underlying HTML has changed a bit:

<p>
   <b>Country</b><br />
   <select id="person_country" name="person[country]"><option
value="Canada">Canada</option>
<option value="Mexico">Mexico</option>

<option value="UK">United Kingdom</option>
<option value="USA">United States of America</option></select>
</p>

The new value attributes for the United Kingdom and United States of America reflect the explicit choices made in the underlying array.

You can also set a default choice for your selections by adding a selected named parameter:

<%= f.select (:country, [ ['Canada', 'Canada'],
                             ['Mexico', 'Mexico'],
                             ['United Kingdom', 'UK'],
                             ['United States of America', 'USA'] ], 
                        :selected => 'USA')%>

This generates the same markup, except that the option element for USA now looks like:

<option value="USA" selected="selected">United States of America</option>

You can also use select with a hash, instead of specifying the array. Example 6-5 shows how this looks much like it did for the radio buttons in Example 6-4.

Example 6-5. Creating a sorted selection list from a hash

<% nations = { 'United States of America' => 'USA', 'Canada' => 'Canada', 'Mexico'
=> 'Mexico', 'United Kingdom' => 'UK' }%>

  <p>
    <b>Country</b><br />
        <% list = nations.sort %>
        <%= f.select :country, list %>
  </p>

Rails also offers a number of specific selection fields, including one for countries (country_select) and one for time zones (time_zone_select). Additionally, if you decide that you want to get really fancy, you can create multilevel selection lists with option_groups_from_collection_for_select. You can also create selection lists that let users choose multiple values by setting the :multiple option to true.

The country_select method has proven a bit controversial, mostly because of the base list of countries it uses. Future versions of Rails may be moving it out of the main framework and into a plug-in.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset