From 450257c95b0ca2c61eca2cefbcb2afd69e90f4a9 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 25 Oct 2011 17:44:17 -0700 Subject: [PATCH] properly handle lists of lists. Thanks @adrianpike for reporting! --- .../lib/action_view/helpers/form_options_helper.rb | 7 ++++++- actionpack/test/template/form_options_helper_test.rb | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/actionpack/lib/action_view/helpers/form_options_helper.rb b/actionpack/lib/action_view/helpers/form_options_helper.rb index d636702111..1a6c3b9740 100644 --- a/actionpack/lib/action_view/helpers/form_options_helper.rb +++ b/actionpack/lib/action_view/helpers/form_options_helper.rb @@ -579,7 +579,12 @@ module ActionView def to_select_tag(choices, options, html_options) selected_value = options.has_key?(:selected) ? options[:selected] : value(object) - if !choices.empty? && Array === choices.first + # Grouped choices look like this: + # + # [nil, []] + # { nil => [] } + # + if !choices.empty? && choices.first.last.respond_to?(:each) option_tags = grouped_options_for_select(choices, :selected => selected_value, :disabled => options[:disabled]) else option_tags = options_for_select(choices, :selected => selected_value, :disabled => options[:disabled]) diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index d3e0cc41a9..469718e1bd 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -596,6 +596,15 @@ class FormOptionsHelperTest < ActionView::TestCase ) end + def test_list_of_lists + @post = Post.new + @post.category = "" + assert_dom_equal( + "", + select("post", "category", [["Number", "number"], ["Text", "text"], ["Yes/No", "boolean"]], :prompt => true, :include_blank => true) + ) + end + def test_select_with_selected_value @post = Post.new @post.category = ""