From 2a6f208ee15479fb1b9beabccf548a9ffb27d589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Fri, 2 Nov 2012 15:21:35 -0700 Subject: Merge pull request #8108 from Casecommons/fix-multiple-and-index-in-instance-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 --- actionpack/CHANGELOG.md | 18 ++++++++++++++++++ actionpack/lib/action_view/helpers/form_helper.rb | 4 +++- actionpack/test/template/form_helper_test.rb | 13 +++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 2bf3801b7d..fd9cd55bc9 100644 --- a/actionpack/CHANGELOG.md +++ b/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) + #=> + + After: + + check_box("post", "comment_ids", { :multiple => true, :index => "foo" }, 1) + #=> + + Fix #8108 + + *Daniel Fox, Grant Hutchins & Trace Wax* + ## Rails 3.2.9 (unreleased) ## * Clear url helpers when reloading routes. diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index a3409ee3c7..d00bad7608 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -1205,9 +1205,11 @@ module ActionView 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 diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 19af01e2c8..49a325af79 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -393,6 +393,19 @@ class FormHelperTest < ActionView::TestCase ) end + def test_check_box_with_multiple_behavior_and_index + @post.comment_ids = [2,3] + assert_dom_equal( + '', + check_box("post", "comment_ids", { :multiple => true, :index => "foo" }, 1) + ) + assert_dom_equal( + '', + check_box("post", "comment_ids", { :multiple => true, :index => "bar" }, 3) + ) + + end + def test_checkbox_disabled_disables_hidden_field assert_dom_equal( '', -- cgit v1.2.3