From ebeeceeb2aa34d33337f991607debae715ca061a Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Tue, 27 Dec 2016 12:47:31 +0900 Subject: [PATCH] do not mutate `object_name` of class in `fields_for` Since it affects the entire form, should not mutate `object_name` of class. Fixes #26931 --- actionview/CHANGELOG.md | 6 ++++++ actionview/lib/action_view/helpers/form_helper.rb | 3 ++- actionview/test/template/form_helper_test.rb | 4 +++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 8da2e4ae1d..2a38ca7b63 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,9 @@ +* Return correct object name in form helper method after `fields_for`. + + Fixes #26931. + + *Yuji Yaginuma* + * Use `ActionView::Resolver.caching?` (`config.action_view.cache_template_loading`) to enable template recompilation. diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb index a66a56d441..26a625e4fe 100644 --- a/actionview/lib/action_view/helpers/form_helper.rb +++ b/actionview/lib/action_view/helpers/form_helper.rb @@ -1924,10 +1924,11 @@ module ActionView record_name = model_name_from_record_or_class(record_object).param_key end + object_name = @object_name index = if options.has_key?(:index) options[:index] elsif defined?(@auto_index) - self.object_name = @object_name.to_s.sub(/\[\]$/, "") + object_name = object_name.to_s.sub(/\[\]$/, "") @auto_index end diff --git a/actionview/test/template/form_helper_test.rb b/actionview/test/template/form_helper_test.rb index 4850bc390b..3da3b95114 100644 --- a/actionview/test/template/form_helper_test.rb +++ b/actionview/test/template/form_helper_test.rb @@ -2262,11 +2262,13 @@ class FormHelperTest < ActionView::TestCase concat f.fields_for("comment[]", @comment) { |c| concat c.text_field(:name) } + concat f.text_field(:body) end expected = whole_form("/posts/123", "edit_post[]", "edit_post[]", method: "patch") do "" + - "" + "" + + "" end assert_dom_equal expected, output_buffer