Module: NestedForm::BuilderMixin
- Included in:
- Builder
- Defined in:
- lib/nested_form/builder_mixin.rb
Instance Method Summary collapse
- #fields_for_nested_model(name, object, options, block) ⇒ Object
- #fields_for_with_nested_attributes(association_name, *args) ⇒ Object
-
#link_to_add(*args, &block) ⇒ Object
Adds a link to insert a new associated records.
-
#link_to_remove(*args, &block) ⇒ Object
Adds a link to remove the associated record.
Instance Method Details
#fields_for_nested_model(name, object, options, block) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/nested_form/builder_mixin.rb', line 59 def fields_for_nested_model(name, object, , block) output = '<div class="fields">'.html_safe # if :hide=true is passed, hide the div if !.nil? and ![:hide].nil? and [:hide]==true output = "<div class=\"fields\" style=\"display:none\"}>".html_safe end output << super output.safe_concat('</div>') output end |
#fields_for_with_nested_attributes(association_name, *args) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/nested_form/builder_mixin.rb', line 51 def fields_for_with_nested_attributes(association_name, *args) # TODO Test this better block = args.pop || 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 |
#link_to_add(*args, &block) ⇒ Object
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.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/nested_form/builder_mixin.rb', line 14 def link_to_add(*args, &block) = args..symbolize_keys association = args.pop [:class] = [[:class], "add_nested_fields"].compact.join(" ") ["data-association"] = association args << (.delete(:href) || "javascript:void(0)") args << @fields ||= {} @template.after_nested_form(association) do model_object = object.class.reflect_on_association(association).klass.new output = %Q[<div id="#{association}_fields_blueprint" style="display: none">].html_safe output << fields_for(association, model_object, :child_index => "new_#{association}", &@fields[association]) output.safe_concat('</div>') output end @template.link_to(*args, &block) end |
#link_to_remove(*args, &block) ⇒ Object
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.
43 44 45 46 47 48 49 |
# File 'lib/nested_form/builder_mixin.rb', line 43 def link_to_remove(*args, &block) = args..symbolize_keys [:class] = [[:class], "remove_nested_fields"].compact.join(" ") args << (.delete(:href) || "javascript:void(0)") args << hidden_field(:_destroy) + @template.link_to(*args, &block) end |