module NestedForm class Builder < ::ActionView::Helpers::FormBuilder # Adds a link to insert a new associated records. The first argument is the name of the link, the second is the name of the association. # # f.link_to_add("Add Task", :tasks) # # You can pass HTML options in a hash at the end and a block for the content. # # <%= f.link_to_add(:tasks, :class => "add_task", :href => new_task_path) do %> # Add Task # <% end %> # # See the README for more details on where to call this method. def link_to_add(*args, &block) options = args.extract_options!.symbolize_keys association = args.pop options[:class] = [options[:class], "add_nested_fields"].compact.join(" ") options["data-association"] = association args << (options.delete(:href) || "javascript:void(0)") args << options @fields ||= {} @template.after_nested_form(association) do model_object = object.class.reflect_on_association(association).klass.new output = %Q[
') output end @template.link_to(*args, &block) end # Adds a link to remove the associated record. The first argment is the name of the link. # # f.link_to_remove("Remove Task") # # You can pass HTML options in a hash at the end and a block for the content. # # <%= f.link_to_remove(:class => "remove_task", :href => "#") do %> # Remove Task # <% end %> # # See the README for more details on where to call this method. def link_to_remove(*args, &block) options = args.extract_options!.symbolize_keys options[:class] = [options[:class], "remove_nested_fields"].compact.join(" ") args << (options.delete(:href) || "javascript:void(0)") args << options hidden_field(:_destroy) + @template.link_to(*args, &block) end def fields_for_with_nested_attributes(association_name, args, block) # TODO Test this better block ||= Proc.new { |fields| @template.render(:partial => "#{association_name.to_s.singularize}_fields", :locals => {:f => fields}) } @fields ||= {} @fields[association_name] = block super(association_name, args, block) end def fields_for_nested_model(name, object, options, block) output = '