Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #12662 from nashby/include-hidden-collection

add include_hidden option to collection_check_boxes helper

Conflicts:
	actionview/CHANGELOG.md
	actionview/test/template/form_collections_helper_test.rb
  • Loading branch information...
commit 2d171bdc89b1a29c098477669e14d92b534baf7e 2 parents e3b12f6 + 106c988
@rafaelfranca rafaelfranca authored
View
4 actionview/CHANGELOG.md
@@ -1,3 +1,7 @@
+* Add `include_hidden` option to `collection_check_boxes` helper.
+
+ *Vasiliy Ermolovich*
+
* Fixed a problem where the default options for the `button_tag` helper is not
applied correctly.
View
10 actionview/lib/action_view/helpers/tags/collection_check_boxes.rb
@@ -27,10 +27,14 @@ 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)
+ 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
+ else
+ rendered_collection
+ end
end
private
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 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
+
+ assert_select "input[type=hidden][name='user[category_ids][]'][value=]", :count => 0
+ 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
Please sign in to comment.
Something went wrong with that request. Please try again.