Skip to content

Instantly share code, notes, and snippets.

@stuartchaney
Last active December 27, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartchaney/7274894 to your computer and use it in GitHub Desktop.
Save stuartchaney/7274894 to your computer and use it in GitHub Desktop.
Updated for newest version of nested_form gem
module NestedForm
module BuilderMixin
def link_to_add_hmt(*args, &block)
options = args.extract_options!.symbolize_keys
association = args.pop
association_two = args.pop
unless object.respond_to?("#{association}_attributes=")
raise ArgumentError, "Invalid association. Make sure that accepts_nested_attributes_for is used for #{association.inspect} association."
end
model_object = options.delete(:model_object) do
reflection = object.class.reflect_on_association(association)
reflection.klass.new
end
options[:class] = [options[:class], "add_nested_fields"].compact.join(" ")
options["data-association"] = association
options["data-blueprint-id"] = fields_blueprint_id = fields_blueprint_id_for(association)
args << (options.delete(:href) || "javascript:void(0)")
args << options
model_object.send("build_#{association_two}".to_sym)
@fields ||= {}
@template.after_nested_form(fields_blueprint_id) do
blueprint = {:id => fields_blueprint_id, :style => 'display: none'}
block, options = @fields[fields_blueprint_id].values_at(:block, :options)
options[:child_index] = "new_#{association}"
blueprint[:"data-blueprint"] = fields_for(association, model_object, options, &block).to_str
@template.content_tag(:div, nil, blueprint)
end
@template.link_to(*args, &block)
end
end
end
@fabricioflores
Copy link

great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment