Browse files

Allow strings to be sent as collection to select.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#2391 state:committed]
  • Loading branch information...
1 parent f7cb7fc commit 66ead4f148964ad7af33f5e44e79fa50a7a00f61 @josevalim josevalim committed with NZKoz Apr 1, 2009
View
2 actionpack/lib/action_view/helpers/form_options_helper.rb
@@ -230,6 +230,8 @@ def time_zone_select(object, method, priority_zones = nil, options = {}, html_op
#
# NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
def options_for_select(container, selected = nil)
+ return container if String === container
+
container = container.to_a if Hash === container
selected, disabled = extract_selected_and_disabled(selected)
View
22 actionpack/test/template/form_options_helper_test.rb
@@ -80,6 +80,14 @@ def test_collection_options_with_proc_for_disabled
)
end
+ def test_string_options_for_select
+ options = "<option value=\"Denmark\">Denmark</option><option value=\"USA\">USA</option><option value=\"Sweden\">Sweden</option>"
+ assert_dom_equal(
+ options,
+ options_for_select(options)
+ )
+ end
+
def test_array_options_for_select
assert_dom_equal(
"<option value=\"&lt;Denmark&gt;\">&lt;Denmark&gt;</option>\n<option value=\"USA\">USA</option>\n<option value=\"Sweden\">Sweden</option>",
@@ -324,6 +332,20 @@ def @post.to_param; 108; end
)
end
+ def test_select_under_fields_for_with_string_and_given_prompt
+ @post = Post.new
+ options = "<option value=\"abe\">abe</option><option value=\"mus\">mus</option><option value=\"hest\">hest</option>"
+
+ fields_for :post, @post do |f|
+ concat f.select(:category, options, :prompt => 'The prompt')
+ end
+
+ assert_dom_equal(
+ "<select id=\"post_category\" name=\"post[category]\"><option value=\"\">The prompt</option>\n#{options}</select>",
+ output_buffer
+ )
+ end
+
def test_select_with_blank
@post = Post.new
@post.category = "<mus>"

0 comments on commit 66ead4f

Please sign in to comment.