diff --git a/actionview/lib/action_view/helpers/tags/collection_helpers.rb b/actionview/lib/action_view/helpers/tags/collection_helpers.rb index 62060547deb44..c2a6a0eafca9a 100644 --- a/actionview/lib/action_view/helpers/tags/collection_helpers.rb +++ b/actionview/lib/action_view/helpers/tags/collection_helpers.rb @@ -106,7 +106,8 @@ def render_collection_for(builder_class, &block) def hidden_field hidden_name = @html_options[:name] || hidden_field_name - @template_object.hidden_field_tag(hidden_name, "", id: nil) + options = { id: nil, form: @html_options[:form] } + @template_object.hidden_field_tag(hidden_name, "", options) end def hidden_field_name diff --git a/actionview/test/template/form_collections_helper_test.rb b/actionview/test/template/form_collections_helper_test.rb index 56d2ec65722a2..3e840f3c3abaf 100644 --- a/actionview/test/template/form_collections_helper_test.rb +++ b/actionview/test/template/form_collections_helper_test.rb @@ -272,6 +272,13 @@ def with_collection_check_boxes(*args, &block) assert_select "input[type=hidden][name='user[other_category_ids][]'][value=''][autocomplete='off']", count: 1 end + test "collection check boxes generates a hidden field using the given :form in :html_options" do + collection = [Category.new(1, "Category 1"), Category.new(2, "Category 2")] + with_collection_check_boxes :user, :category_ids, collection, :id, :name, {}, { form: "my_form" } + + assert_select "input[type=hidden][value=''][autocomplete='off'][form='my_form']", 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