Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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][]")
    # => <select name="post[category][][]" ...>

After:

    select(:category, [], {}, multiple: true, name: "post[category][]")
    # => <select name="post[category][]" ...>
  • Loading branch information...
commit 8e05a6f6380c6ddbef5a0620b30d19dbca7f2f15 1 parent dd1d309
Olek Janiszewski authored March 08, 2013
15  actionpack/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,20 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   Fix incorrectly appended square brackets to a multiple select box
  4
+    if an explicit name has been given and it already ends with "[]"
  5
+
  6
+    Before:
  7
+
  8
+        select(:category, [], {}, multiple: true, name: "post[category][]")
  9
+        # => <select name="post[category][][]" ...>
  10
+
  11
+    After:
  12
+
  13
+        select(:category, [], {}, multiple: true, name: "post[category][]")
  14
+        # => <select name="post[category][]" ...>
  15
+
  16
+    *Olek Janiszewski*
  17
+
3 18
 *   Fixed regression when using `assert_template` to verify files sent using
4 19
     `render file: 'README.md'`.
5 20
     Fixes #9464.
2  actionpack/lib/action_view/helpers/tags/base.rb
@@ -84,7 +84,7 @@ def add_default_name_and_id(options)
84 84
             options["id"] = options.fetch("id"){ tag_id }
85 85
           end
86 86
 
87  
-          options["name"] += "[]" if options["multiple"]
  87
+          options["name"] += "[]" if options["multiple"] && !options["name"].ends_with?("[]")
88 88
           options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence
89 89
         end
90 90
 
8  actionpack/test/template/form_options_helper_test.rb
@@ -575,6 +575,14 @@ def test_select_with_multiple_and_without_hidden_input
575 575
     )
576 576
   end
577 577
 
  578
+  def test_select_with_multiple_and_with_explicit_name_ending_with_brackets
  579
+    output_buffer =  select(:post, :category, [], {include_hidden: false}, multiple: true, name: 'post[category][]')
  580
+    assert_dom_equal(
  581
+      "<select multiple=\"multiple\" id=\"post_category\" name=\"post[category][]\"></select>",
  582
+      output_buffer
  583
+    )
  584
+  end
  585
+
578 586
   def test_select_with_multiple_and_disabled_to_add_disabled_hidden_input
579 587
     output_buffer =  select(:post, :category, "", {}, :multiple => true, :disabled => true)
580 588
     assert_dom_equal(

0 notes on commit 8e05a6f

Please sign in to comment.
Something went wrong with that request. Please try again.