I have a parent, which I can add multiple children and I would like to add a clone button with cocoon in each child.
Following the next solution I have made this code:
These are the models:
class MantenimientoSetup < ApplicationRecord
has_many :actuation_setups, inverse_of: :mantenimiento_setup, validate: true, autosave: true, dependent: :destroy
accepts_nested_attributes_for :actuation_setups, allow_destroy: true, reject_if: :all_blank
end
class ActuationSetup < ApplicationRecord
belongs_to :mantenimiento_setup, inverse_of: :actuation_setups
end
I have got to add the clone button out of child partial... parent and child form
but I need this button for each new child nested, like this: enter image description here
These are the parent and child partials:
<!-- views/mantenimiento_setups/_form.html.erb -->
<%= form_for @mantenimiento_setup do |f| %>
...
...
...
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">Configuraciones</h4>
</div>
<div class="panel-body mantenimiento-setup-element">
<%= f.fields_for :actuation_setups do |act_setup_builder| -%>
<%= render 'actuation_setup_fields', f: act_setup_builder %>
<%= link_to_add_association 'Clone', f, :actuation_setups, data: {"association-insertion-node" => ".mantenimiento-setup-element", "association-insertion-method" => "append"}, wrap_object: Proc.new {|d| d = act_setup_builder.object.dup; d}, class: "btn btn-info btn-sm" %>
<% end -%>
</div>
<div class="panel-footer">
<%= link_to_add_fields 'Add new setup', f, :actuation_setups, {mantenimiento_setup: f.object}, {class: "btn btn-success", nested_form: "/mantenimiento_setups/actuation_setup_fields" }%>
</div>
</div>
<%= save_button %>
<% end %>
<!-- views/mantenimiento_setups/_actuation_setup_fields.html.erb -->
<div class='row fields'>
<div class="col-md-3 actuation-type-div-selector">
...
...
...
<div class="col-md-1">
<label class="control-label invisible">...</label><br/>
<%= link_to_remove_fields "Remove", f%>
</div>
</div>
If I paste the clone button in the child partial, close to remove button...
<!-- views/mantenimiento_setups/_actuation_setup_fields.html.erb -->
<div class='row fields'>
<div class="col-md-3 actuation-type-div-selector">
...
...
...
<div class="col-md-1">
<label class="control-label invisible">...</label><br/>
<%= link_to_remove_fields "Remove", f%>
</div>
<div class="col-md-1">
<label class="control-label invisible">...</label><br/>
<%= link_to_add_association 'Clone', f, :actuation_setups, data: {"association-insertion-node" => ".mantenimiento-setup-element", "association-insertion-method" => "append"}, wrap_object: Proc.new {|d| d = act_setup_builder.object.dup; d}, class: "btn btn-info btn-sm" %>
</div>
</div>
It returns this error because the association does not exist due to I am calling from child to child association:
Association actuation_setups doesn't exist on ActuationSetup
How can I add a clone button for each child?