Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

options_for_select with html_options

#661

This patch allows you to specify HTML attributes to the option elements generated by the options_for_select method and methods alike. This is useful if you want, for example, have altering backgrounds for each select option.

The html_option parameter can be a hash which applies the attributes to all option elements, like so:

options_for_select({"Dollar" => "$", "Kroner" => "DKK", "Kronor" => "SEK"}, "Kronor", :style => 'color: blue;') # => <option value="$" style="color: blue;">Dollar</option>\n<option value="DKK" style="color: blue;">Kroner</option>\n<option value="SEK" style="color: blue;">Kronor</option>

or it can be a proc to calculate the attributes for each option, like so:

options_for_select({"Dollar" => "$", "Kroner" => "DKK", "Kronor" => "SEK"}, "Kronor", lambda { |text, value, selected| {:class => cycle("odd", "even")} }) # => <option value="$" class="odd">Dollar</option>\n<option value="DKK" class="even">Kroner</option>\n<option value="SEK" class="odd">Kronor</option>

This patch also alters an existing test: test_hash_options_for_select. The expected result in the test was sorted by string comparison which does not work with the changes I made. The test's expected result is now unchanged and the result from options_for_select now expects the generated options to be sorted by its display text.

A comment has been added to the documentation about this change of expectation.

Reported by Arthur Carlsson · July 19th, 2008 @ 12:12 PM

State: stale
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. josh
    josh
    • State changed from new to stale

    Staling out, please reopen if you are still interested.

    October 28th, 2008 @ 04:28 PM

  2. bingbing