Skip to content

Commit

Permalink
Move the hidden :id field logic to where it belongs to
Browse files Browse the repository at this point in the history
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
carlosantoniodasilva committed Jan 6, 2013
1 parent 04338b9 commit e56e3db
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions actionpack/lib/action_view/helpers/form_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -705,9 +705,7 @@ def apply_form_for_options!(record, object, options) #:nodoc:
# to prevent fields_for from rendering it automatically. # to prevent fields_for from rendering it automatically.
def fields_for(record_name, record_object = nil, options = {}, &block) def fields_for(record_name, record_object = nil, options = {}, &block)
builder = instantiate_builder(record_name, record_object, options) builder = instantiate_builder(record_name, record_object, options)
output = capture(builder, &block) capture(builder, &block)
output.concat builder.hidden_field(:id) if output && options[:hidden_field_id] && !builder.emitted_hidden_id?
output
end end


# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object # Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
Expand Down Expand Up @@ -1824,9 +1822,14 @@ def fields_for_nested_model(name, object, options, block)
object = convert_to_model(object) object = convert_to_model(object)


parent_include_id = self.options.fetch(:include_id, true) parent_include_id = self.options.fetch(:include_id, true)
include_id = options.fetch(:include_id, parent_include_id) include_id = options.fetch(:include_id, parent_include_id)
options[:hidden_field_id] = object.persisted? && include_id hidden_field_id = object.persisted? && include_id
@template.fields_for(name, object, options, &block)
@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 end


def nested_child_index(name) def nested_child_index(name)
Expand Down

0 comments on commit e56e3db

Please sign in to comment.