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.

Create optgroups in options_for_select

#1986

This is a small patch to enable options_for_select to create optgroups from nested arrays. For example:


   options_for_select([ ["First Group", [["Option 1", 1], ["Option 2", 2]]], ["Bare Option", 3] ])

yields (formatted for readability):


<optgroup label="First Group">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
</optgroup>
<option value="3">Bare Option</option>

Note that the nested things don't have to be arrays; the group label is taken from .first of the element, and the .last of the element can be anything that options_for_select supports.

All the other options work exactly as expected (selected, disabled, etc).

Note that this is slightly more general than grouped_options_for_select, as it can produce mixed sets with both bare options and optgroups.

Test included in the patch.

Reported by Matt Jones · February 16th, 2009 @ 07:59 AM

State: wontfix
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. DHH
    DHH
    • Milestone set to 2.x

    February 22nd, 2009 @ 03:09 PM

  2. Pratik
    Pratik
    • Assigned user set to Pratik
    • State changed from new to wontfix

    I think we should just add a way for grouped_options_for_select() to generated the desired output here. The form options helpers API is getting way too complex for my liking, not sure I like the idea of adding one more helper.

    March 9th, 2009 @ 01:55 PM