From 8761663a68bd7ddd918f78fb3def4697784024f2 Mon Sep 17 00:00:00 2001 From: Jon Crawford Date: Thu, 29 Jan 2009 17:59:44 +0000 Subject: [PATCH] Added grouped_options_for_select helper method for wrapping option tags in optgroups. [#977 state:resolved] Signed-off-by: Pratik Naik --- actionpack/CHANGELOG | 2 + .../helpers/form_options_helper.rb | 56 +++++++++++++++++++ .../test/template/form_options_helper_test.rb | 26 +++++++++ 3 files changed, 84 insertions(+) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 6bcb595cdb6c1..e9e18a8f6b6c4 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *2.3.0 [Edge]* +* Added grouped_options_for_select helper method for wrapping option tags in optgroups. #977 [Jon Crawford] + * Implement HTTP Digest authentication. #1230 [Gregg Kellogg, Pratik Naik] Example : class DummyDigestController < ActionController::Base diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index 3991c5c0cc45e..54c82cbd1dec4 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -277,6 +277,62 @@ def option_groups_from_collection_for_select(collection, group_method, group_lab end end + # Returns a string of tags, like options_for_select, but + # wraps them with tags. + # + # Parameters: + # * +grouped_options+ - Accepts a nested array or hash of strings. The first value serves as the + # label while the second value must be an array of options. The second value can be a + # nested array of text-value pairs. See options_for_select for more info. + # Ex. ["North America",[["United States","US"],["Canada","CA"]]] + # * +selected_key+ - A value equal to the +value+ attribute for one of the tags, + # which will have the +selected+ attribute set. Note: It is possible for this value to match multiple options + # as you might have the same option in multiple groups. Each will then get selected="selected". + # * +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. + # + # Sample usage (Array): + # grouped_options = [ + # ['North America', + # [['United States','US'],'Canada']], + # ['Europe', + # ['Denmark','Germany','France']] + # ] + # grouped_options_for_select(grouped_options) + # + # Sample usage (Hash): + # grouped_options = { + # 'North America' => [['United States','US], 'Canada'], + # 'Europe' => ['Denmark','Germany','France'] + # } + # grouped_options_for_select(grouped_options) + # + # Possible output: + # + # + # + # + # + # + # + # + # + # + # Note: Only the and tags are returned, so you still have to + # wrap the output in an appropriate