Permalink
Browse files

Merge pull request #14736 from nashby/hidden-index

`collection_check_boxes` respects `:index` option for the hidden filed name
Conflicts:
	actionview/lib/action_view/helpers/tags/collection_check_boxes.rb
	actionview/test/template/form_collections_helper_test.rb
  • Loading branch information...
1 parent aaeff0e commit c07bfd3fe738e9d4b5234ad6bd356e1b0295da30 @rafaelfranca rafaelfranca committed Apr 14, 2014
View
6 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*
View
17 actionview/lib/action_view/helpers/tags/collection_check_boxes.rb
@@ -27,17 +27,26 @@ def render(&block)
# Append a hidden field to make sure something will be sent back to the
# server if all check boxes are unchecked.
- hidden_name = @html_options[:name] || "#{tag_name}[]"
- hidden = @template_object.hidden_field_tag(hidden_name, "", :id => nil)
-
- rendered_collection + hidden
+ rendered_collection + hidden_field
end
private
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
View
7 actionview/test/template/form_collections_helper_test.rb
@@ -204,6 +204,13 @@ def with_collection_check_boxes(*args, &block)
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 accepts a collection and generate a series of checkboxes with labels for label method' do
collection = [Category.new(1, 'Category 1'), Category.new(2, 'Category 2')]
with_collection_check_boxes :user, :category_ids, collection, :id, :name

0 comments on commit c07bfd3

Please sign in to comment.