Permalink
Browse files

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.
  • Loading branch information...
1 parent 04338b9 commit e56e3db6e6c3ed1618311122c51066a8ac8beb0e @carlosantoniodasilva carlosantoniodasilva committed Jan 3, 2013
Showing with 9 additions and 6 deletions.
  1. +9 −6 actionpack/lib/action_view/helpers/form_helper.rb
View
15 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)

0 comments on commit e56e3db

Please sign in to comment.