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]: 5e1a039a1d/actionview/lib/action_view/helpers/form_helper.rb (L2235)
[form_with]: 5e1a039a1d/actionview/lib/action_view/helpers/form_helper.rb (L749)
[form_for]: 5e1a039a1d/actionview/lib/action_view/helpers/form_helper.rb (L436)
This commit is contained in:
Sean Doyle 2021-10-08 21:36:49 -04:00
parent 9f980664fc
commit 46175a036a
3 changed files with 16 additions and 0 deletions

View File

@ -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 })

View File

@ -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

View File

@ -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 = %(<input type="text" value="new comment" name="comment[name]" id="comment_name" />)
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)