Skip to content

Commit f978e1d

Browse files
authored
Merge pull request rails#51746 from 3v0k4/hidden-form
Use the given `form` in `html_options` for the hidden field in `collection_check_boxes`
2 parents a4b202f + d4702f9 commit f978e1d

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

actionview/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Respect `html_options[:form]` when `collection_checkboxes` generates the
2+
hidden `<input>`.
3+
4+
*Riccardo Odone*
5+
16
* Layouts have access to local variables passed to `render`.
27

38
This fixes #31680 which was a regression in Rails 5.1.

actionview/lib/action_view/helpers/tags/collection_helpers.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ def render_collection_for(builder_class, &block)
106106

107107
def hidden_field
108108
hidden_name = @html_options[:name] || hidden_field_name
109-
@template_object.hidden_field_tag(hidden_name, "", id: nil)
109+
options = { id: nil, form: @html_options[:form] }
110+
@template_object.hidden_field_tag(hidden_name, "", options)
110111
end
111112

112113
def hidden_field_name

actionview/test/template/form_collections_helper_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,13 @@ def with_collection_checkboxes(*args, &block)
272272
assert_select "input[type=hidden][name='user[other_category_ids][]'][value=''][autocomplete='off']", count: 1
273273
end
274274

275+
test "collection check boxes generates a hidden field using the given :form in :html_options" do
276+
collection = [Category.new(1, "Category 1"), Category.new(2, "Category 2")]
277+
with_collection_checkboxes :user, :category_ids, collection, :id, :name, {}, { form: "my_form" }
278+
279+
assert_select "input[type=hidden][value=''][autocomplete='off'][form='my_form']", count: 1
280+
end
281+
275282
test "collection check boxes generates a hidden field with index if it was provided" do
276283
collection = [Category.new(1, "Category 1"), Category.new(2, "Category 2")]
277284
with_collection_checkboxes :user, :category_ids, collection, :id, :name, index: 322

0 commit comments

Comments
 (0)