Skip to content

Commit 8e05a6f

Browse files
committed
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][]" ...>
1 parent dd1d309 commit 8e05a6f

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

actionpack/CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
## Rails 4.0.0 (unreleased) ##
22

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+
318
* Fixed regression when using `assert_template` to verify files sent using
419
`render file: 'README.md'`.
520
Fixes #9464.

actionpack/lib/action_view/helpers/tags/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def add_default_name_and_id(options)
8484
options["id"] = options.fetch("id"){ tag_id }
8585
end
8686

87-
options["name"] += "[]" if options["multiple"]
87+
options["name"] += "[]" if options["multiple"] && !options["name"].ends_with?("[]")
8888
options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence
8989
end
9090

actionpack/test/template/form_options_helper_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,14 @@ def test_select_with_multiple_and_without_hidden_input
575575
)
576576
end
577577

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+
578586
def test_select_with_multiple_and_disabled_to_add_disabled_hidden_input
579587
output_buffer = select(:post, :category, "", {}, :multiple => true, :disabled => true)
580588
assert_dom_equal(

0 commit comments

Comments
 (0)