Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrectly appended square brackets to a multiple select box #9616

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
## Rails 4.0.0 (unreleased) ##

* 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][]" ...>

*Olek Janiszewski*

* Fixed regression when using `assert_template` to verify files sent using
`render file: 'README.md'`.
Fixes #9464.
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_view/helpers/tags/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def add_default_name_and_id(options)
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

Expand Down
8 changes: 8 additions & 0 deletions actionpack/test/template/form_options_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,14 @@ def test_select_with_multiple_and_without_hidden_input
)
end

def test_select_with_multiple_and_with_explicit_name_ending_with_brackets
output_buffer = select(:post, :category, [], {include_hidden: false}, multiple: true, name: 'post[category][]')
assert_dom_equal(
"<select multiple=\"multiple\" id=\"post_category\" name=\"post[category][]\"></select>",
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(
Expand Down