From 46175a036a6a3821fe292f6cba73de030b0f8434 Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Fri, 8 Oct 2021 21:36:49 -0400 Subject: [PATCH] Action View: Support `fields model: [...]` Support `fields model: [@nested, @model]` the same way as `form_with model: [@nested, @model]`. After this change, the `fields` helper matches the precedent established by [fields_for][], [form_for][], and [form_with][]. [fields_for]: https://github.com/rails/rails/blob/5e1a039a1dd63ab70300a1340226eab690444cea/actionview/lib/action_view/helpers/form_helper.rb#L2235 [form_with]: https://github.com/rails/rails/blob/5e1a039a1dd63ab70300a1340226eab690444cea/actionview/lib/action_view/helpers/form_helper.rb#L749 [form_for]: https://github.com/rails/rails/blob/5e1a039a1dd63ab70300a1340226eab690444cea/actionview/lib/action_view/helpers/form_helper.rb#L436 --- actionview/CHANGELOG.md | 5 +++++ actionview/lib/action_view/helpers/form_helper.rb | 1 + actionview/test/template/form_helper/form_with_test.rb | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 21af906bcc..e32c4c5721 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,8 @@ +* Support `fields model: [@nested, @model]` the same way as `form_with model: + [@nested, @model]`. + + *Sean Doyle* + * Add `:day_format` option to `date_select` date_select("article", "written_on", day_format: ->(day) { day.ordinalize }) diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb index ab7e88f92c..5f291ea438 100644 --- a/actionview/lib/action_view/helpers/form_helper.rb +++ b/actionview/lib/action_view/helpers/form_helper.rb @@ -1059,6 +1059,7 @@ module ActionView options[:skip_default_ids] = !form_with_generates_ids if model + model = model.last if model.is_a?(Array) scope ||= model_name_from_record_or_class(model).param_key end diff --git a/actionview/test/template/form_helper/form_with_test.rb b/actionview/test/template/form_helper/form_with_test.rb index f019e53127..13b00f8a23 100644 --- a/actionview/test/template/form_helper/form_with_test.rb +++ b/actionview/test/template/form_helper/form_with_test.rb @@ -2033,6 +2033,16 @@ class FormWithActsLikeFormForTest < FormWithTest assert_dom_equal expected, output_buffer end + def test_fields_with_only_object_array + output_buffer = fields(model: [@post, @comment]) do |f| + concat f.text_field(:name) + end + + expected = %() + + assert_dom_equal expected, output_buffer + end + def test_fields_object_with_bracketed_name output_buffer = fields("author[post]", model: @post) do |f| concat f.label(:title)