Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #8108 from Casecommons/fix-multiple-and-index-in-i…
…nstance-tag

Support :multiple option on input tags that also have :index
Conflicts:
	actionpack/lib/action_view/helpers/tags/base.rb
	actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb
  • Loading branch information
rafaelfranca committed Nov 8, 2012
1 parent b025567 commit 2a6f208
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
18 changes: 18 additions & 0 deletions actionpack/CHANGELOG.md
@@ -1,3 +1,21 @@
## Rails 3.2.10 (unreleased) ##

* Fix input name when `:multiple => true` and `:index` are set.

Before:

check_box("post", "comment_ids", { :multiple => true, :index => "foo" }, 1)
#=> <input name=\"post[foo][comment_ids]\" type=\"hidden\" value=\"0\" /><input id=\"post_foo_comment_ids_1\" name=\"post[foo][comment_ids]\" type=\"checkbox\" value=\"1\" />

After:

check_box("post", "comment_ids", { :multiple => true, :index => "foo" }, 1)
#=> <input name=\"post[foo][comment_ids][]\" type=\"hidden\" value=\"0\" /><input id=\"post_foo_comment_ids_1\" name=\"post[foo][comment_ids][]\" type=\"checkbox\" value=\"1\" />

Fix #8108

*Daniel Fox, Grant Hutchins & Trace Wax*

## Rails 3.2.9 (unreleased) ##

* Clear url helpers when reloading routes.
Expand Down
4 changes: 3 additions & 1 deletion actionpack/lib/action_view/helpers/form_helper.rb
Expand Up @@ -1205,9 +1205,11 @@ def add_default_name_and_id(options)
options["name"] ||= tag_name_with_index(@auto_index)
options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) }
else
options["name"] ||= tag_name + (options['multiple'] ? '[]' : '')
options["name"] ||= tag_name
options["id"] = options.fetch("id"){ tag_id }
end

options["name"] += "[]" if options["multiple"]
options["id"] = [options.delete('namespace'), options["id"]].compact.join("_").presence
end

Expand Down
13 changes: 13 additions & 0 deletions actionpack/test/template/form_helper_test.rb
Expand Up @@ -393,6 +393,19 @@ def test_check_box_with_multiple_behavior
)
end

def test_check_box_with_multiple_behavior_and_index
@post.comment_ids = [2,3]
assert_dom_equal(
'<input name="post[foo][comment_ids][]" type="hidden" value="0" /><input id="post_foo_comment_ids_1" name="post[foo][comment_ids][]" type="checkbox" value="1" />',
check_box("post", "comment_ids", { :multiple => true, :index => "foo" }, 1)
)
assert_dom_equal(
'<input name="post[bar][comment_ids][]" type="hidden" value="0" /><input checked="checked" id="post_bar_comment_ids_3" name="post[bar][comment_ids][]" type="checkbox" value="3" />',
check_box("post", "comment_ids", { :multiple => true, :index => "bar" }, 3)
)

end

def test_checkbox_disabled_disables_hidden_field
assert_dom_equal(
'<input name="post[secret]" type="hidden" value="0" disabled="disabled"/><input checked="checked" disabled="disabled" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
Expand Down

0 comments on commit 2a6f208

Please sign in to comment.