This project is archived and is in readonly mode.

#1986 ✓wontfix
Matt Jones

Create optgroups in options_for_select

Reported by Matt Jones | February 16th, 2009 @ 07:59 AM | in 2.x

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.

Comments and changes to this ticket

  • DHH

    DHH February 22nd, 2009 @ 03:09 PM

    • Milestone set to 2.x
  • Pratik

    Pratik March 9th, 2009 @ 01:55 PM

    • 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.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Referenced by

Pages