From 488699166c3558963fa82d4689a35f8c3fd93f47 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Sat, 9 Mar 2013 06:48:04 -0800 Subject: Merge pull request #9616 from exviva/multiple_select_name_double_square_brackets Fix incorrectly appended square brackets to a multiple select box Before: select(:category, [], {}, {:multiple => true, :name => "post[category][]"}) # => Conflicts: actionpack/CHANGELOG.md actionpack/lib/action_view/helpers/tags/base.rb actionpack/test/template/form_options_helper_test.rb --- actionpack/CHANGELOG.md | 17 +++++++++++++++++ actionpack/lib/action_view/helpers/form_helper.rb | 2 +- actionpack/test/template/form_options_helper_test.rb | 8 ++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 86bee63549..f1eb9e84f6 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -5,6 +5,23 @@ ## Rails 3.2.13 (Feb 17, 2013) ## +* Fix incorrectly appended square brackets to a multiple select box + if an explicit name has been given and it already ends with "[]". + + Before: + + select(:category, [], {}, multiple: true, name: "post[category][]") + # => + + Backport #9616. + + *Olek Janiszewski* + * Determine the controller#action from only the matched path when using the shorthand syntax. Previously the complete path was used, which led to problems with nesting (scopes and namespaces). diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 7df74d96fb..920dc3f794 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -1207,7 +1207,7 @@ module ActionView options["id"] = options.fetch("id"){ tag_id } end - options["name"] += "[]" if options["multiple"] + options["name"] += "[]" if options["multiple"] && !options["name"].ends_with?("[]") options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence end diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb index 3009fa5330..72c2609a48 100644 --- a/actionpack/test/template/form_options_helper_test.rb +++ b/actionpack/test/template/form_options_helper_test.rb @@ -515,6 +515,14 @@ class FormOptionsHelperTest < ActionView::TestCase ) end + def test_select_with_multiple_and_with_explicit_name_ending_with_brackets + output_buffer = select(:post, :category, "", {}, :multiple => true, :name => 'post[category][]') + assert_dom_equal( + "", + output_buffer + ) + end + def test_select_with_multiple_and_disabled_to_add_disabled_hidden_input output_buffer = select(:post, :category, "", {}, :multiple => true, :disabled => true) assert_dom_equal( -- cgit v1.2.3