aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Fox, Grant Hutchins & Trace Wax <pair+dfox+grant+trace@pivotallabs.com>2012-11-02 14:47:35 -0700
committerGrant Hutchins <nertzy@gmail.com>2012-11-02 18:14:04 -0400
commit175ba04cf38134e156208c7e59808c40e8c25aa5 (patch)
tree0ff0203900608db71dfb96c58412ae7c976d18ff
parent743d07ea2c3303b0f400f145f45686848e4bb762 (diff)
downloadrails-175ba04cf38134e156208c7e59808c40e8c25aa5.tar.gz
rails-175ba04cf38134e156208c7e59808c40e8c25aa5.tar.bz2
rails-175ba04cf38134e156208c7e59808c40e8c25aa5.zip
Support :multiple option on input tags with :index
When you have an explicit index set, then when you build an input tag with :multiple => true, it doesn't add [] to the end of its name, although it should.
-rw-r--r--actionpack/lib/action_view/helpers/tags/base.rb8
-rw-r--r--actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb2
-rw-r--r--actionpack/test/template/form_helper_test.rb13
3 files changed, 17 insertions, 6 deletions
diff --git a/actionpack/lib/action_view/helpers/tags/base.rb b/actionpack/lib/action_view/helpers/tags/base.rb
index 192f5eebaa..3d597079c4 100644
--- a/actionpack/lib/action_view/helpers/tags/base.rb
+++ b/actionpack/lib/action_view/helpers/tags/base.rb
@@ -80,9 +80,11 @@ module ActionView
options["name"] ||= options.fetch("name"){ tag_name_with_index(@auto_index) }
options["id"] = options.fetch("id"){ tag_id_with_index(@auto_index) }
else
- options["name"] ||= options.fetch("name"){ options['multiple'] ? tag_name_multiple : tag_name }
+ options["name"] ||= options.fetch("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
@@ -90,10 +92,6 @@ module ActionView
"#{@object_name}[#{sanitized_method_name}]"
end
- def tag_name_multiple
- "#{tag_name}[]"
- end
-
def tag_name_with_index(index)
"#{@object_name}[#{index}][#{sanitized_method_name}]"
end
diff --git a/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb b/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb
index e23f5113fb..45f0bc3d7b 100644
--- a/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb
+++ b/actionpack/lib/action_view/helpers/tags/collection_check_boxes.rb
@@ -27,7 +27,7 @@ module ActionView
# Append a hidden field to make sure something will be sent back to the
# server if all check boxes are unchecked.
- hidden = @template_object.hidden_field_tag(tag_name_multiple, "", :id => nil)
+ hidden = @template_object.hidden_field_tag("#{tag_name}[]", "", :id => nil)
rendered_collection + hidden
end
diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb
index fbfc73deda..c730e3ab74 100644
--- a/actionpack/test/template/form_helper_test.rb
+++ b/actionpack/test/template/form_helper_test.rb
@@ -525,6 +525,19 @@ class FormHelperTest < ActionView::TestCase
)
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" />',