From e56e3db6e6c3ed1618311122c51066a8ac8beb0e Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 3 Jan 2013 01:11:10 -0200 Subject: [PATCH] Move the hidden :id field logic to where it belongs to When dealing with nested forms, Rails automatically generates a hidden field with the id value of the current object being generated by fields_for. This logic was inside the method that's available from the template object, but we just need it when really dealing with nested attributes, so moving the code to here makes more sense. --- actionpack/lib/action_view/helpers/form_helper.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb index 481ddcb981..d0c8edc901 100644 --- a/actionpack/lib/action_view/helpers/form_helper.rb +++ b/actionpack/lib/action_view/helpers/form_helper.rb @@ -705,9 +705,7 @@ def apply_form_for_options!(record, object, options) #:nodoc: # to prevent fields_for from rendering it automatically. def fields_for(record_name, record_object = nil, options = {}, &block) builder = instantiate_builder(record_name, record_object, options) - output = capture(builder, &block) - output.concat builder.hidden_field(:id) if output && options[:hidden_field_id] && !builder.emitted_hidden_id? - output + capture(builder, &block) end # Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object @@ -1824,9 +1822,14 @@ def fields_for_nested_model(name, object, options, block) object = convert_to_model(object) parent_include_id = self.options.fetch(:include_id, true) - include_id = options.fetch(:include_id, parent_include_id) - options[:hidden_field_id] = object.persisted? && include_id - @template.fields_for(name, object, options, &block) + include_id = options.fetch(:include_id, parent_include_id) + hidden_field_id = object.persisted? && include_id + + @template.fields_for(name, object, options) do |f| + output = @template.capture(f, &block) + output.concat f.hidden_field(:id) if output && hidden_field_id && !f.emitted_hidden_id? + output + end end def nested_child_index(name)