aboutsummaryrefslogtreecommitdiffstats
path: root/actionview
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-04-14 11:41:03 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-04-14 11:41:03 -0300
commitd40d39214c565076add65b41434b3f95cc0c3b0d (patch)
treeea82c1bbbafc35731625845856cd8f7a46fb96db /actionview
parent2e000ab23d187e425a1be1ce8ce43a0d4c6f5be7 (diff)
parent3964bbc490bdd858b431862bdefa58f477cb2028 (diff)
downloadrails-d40d39214c565076add65b41434b3f95cc0c3b0d.tar.gz
rails-d40d39214c565076add65b41434b3f95cc0c3b0d.tar.bz2
rails-d40d39214c565076add65b41434b3f95cc0c3b0d.zip
Merge pull request #14736 from nashby/hidden-index
`collection_check_boxes` respects `:index` option for the hidden filed name
Diffstat (limited to 'actionview')
-rw-r--r--actionview/CHANGELOG.md6
-rw-r--r--actionview/lib/action_view/helpers/tags/collection_check_boxes.rb17
-rw-r--r--actionview/test/template/form_collections_helper_test.rb7
3 files changed, 26 insertions, 4 deletions
diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md
index 8c6db33be7..0302077e1c 100644
--- a/actionview/CHANGELOG.md
+++ b/actionview/CHANGELOG.md
@@ -1,3 +1,9 @@
+* `collection_check_boxes` respects `:index` option for the hidden filed name.
+
+ Fixes #14147.
+
+ *Vasiliy Ermolovich*
+
* `date_select` helper with option `with_css_classes: true` does not overwrite other classes.
*Izumi Wong-Horiuchi*
diff --git a/actionview/lib/action_view/helpers/tags/collection_check_boxes.rb b/actionview/lib/action_view/helpers/tags/collection_check_boxes.rb
index 8b28e4fc33..6242a2a085 100644
--- a/actionview/lib/action_view/helpers/tags/collection_check_boxes.rb
+++ b/actionview/lib/action_view/helpers/tags/collection_check_boxes.rb
@@ -28,10 +28,7 @@ module ActionView
# Append a hidden field to make sure something will be sent back to the
# server if all check boxes are unchecked.
if @options.fetch(:include_hidden, true)
- hidden_name = @html_options[:name] || "#{tag_name}[]"
- hidden = @template_object.hidden_field_tag(hidden_name, "", :id => nil)
-
- rendered_collection + hidden
+ rendered_collection + hidden_field
else
rendered_collection
end
@@ -42,6 +39,18 @@ module ActionView
def render_component(builder)
builder.check_box + builder.label
end
+
+ def hidden_field
+ hidden_name = @html_options[:name]
+
+ hidden_name ||= if @options.has_key?(:index)
+ "#{tag_name_with_index(@options[:index])}[]"
+ else
+ "#{tag_name}[]"
+ end
+
+ @template_object.hidden_field_tag(hidden_name, "", id: nil)
+ end
end
end
end
diff --git a/actionview/test/template/form_collections_helper_test.rb b/actionview/test/template/form_collections_helper_test.rb
index 8107529149..5e991d87ad 100644
--- a/actionview/test/template/form_collections_helper_test.rb
+++ b/actionview/test/template/form_collections_helper_test.rb
@@ -221,6 +221,13 @@ class FormCollectionsHelperTest < ActionView::TestCase
assert_select "input[type=hidden][name='user[other_category_ids][]'][value=]", :count => 1
end
+ test 'collection check boxes generates a hidden field with index if it was provided' do
+ collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')]
+ with_collection_check_boxes :user, :category_ids, collection, :id, :name, { index: 322 }
+
+ assert_select "input[type=hidden][name='user[322][category_ids][]'][value=]", count: 1
+ end
+
test 'collection check boxes does not generate a hidden field if include_hidden option is false' do
collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')]
with_collection_check_boxes :user, :category_ids, collection, :id, :name, include_hidden: false