Skip to content

Instantly share code, notes, and snippets.

@RSpace
Created May 23, 2010 20:37
Show Gist options
  • Save RSpace/411231 to your computer and use it in GitHub Desktop.
Save RSpace/411231 to your computer and use it in GitHub Desktop.
class ActionView::Helpers::FormBuilder
private
# PATCH: This is a patch of bug in Rails 3 beta 3, and should be removed when we move to Rails 3 final
def fields_for_with_nested_attributes(association_name, args, block)
name = "#{object_name}[#{association_name}_attributes]"
options = args.extract_options!
association = args.shift
association = association.to_model if association.respond_to?(:to_model)
if association.respond_to?(:persisted?)
association = [association] if @object.send(association_name).is_a?(Array)
elsif !association.is_a?(Array)
association = @object.send(association_name)
end
if association.respond_to?(:each) # HACK: Replaces if association.is_a?(Array)
explicit_child_index = options[:child_index]
output = ActiveSupport::SafeBuffer.new
association.each do |child|
output << fields_for_nested_model("#{name}[#{explicit_child_index || nested_child_index(name)}]", child, options, block)
end
output
elsif association
fields_for_nested_model(name, association, options, block)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment