Reputation: 49
My issue is best described with a picture: extra forms added issue
Here is the top model:
class Profession < ApplicationRecord
has_many :procedure_categories, dependent: :destroy
accepts_nested_attributes_for :procedure_categories, allow_destroy: true
end
And second:
class ProcedureCategory < ApplicationRecord
belongs_to :profession
has_many :procedures
accepts_nested_attributes_for :procedures, allow_destroy: true
end
And third:
class Procedure < ApplicationRecord
belongs_to :procedure_category
end
Here is where the code is that is causing the issue including the link_to_add_association method.
<div class="nested-fields">
<%= f.label :category %>
<%= f.text_field :category, class: 'form-control' %>
<%= f.label :description %>
<%= f.text_field :description, class: 'form-control' %>
<%= f.label :display_order %>
<%= f.text_field :display_order, class: 'form-control' %>
<% cs = options_for_select(controls, f.object.selection_type) %>
<%= f.label :selection_type %>
<%= f.select :selection_type, cs, class: 'form-control' %>
<table class='table'>
<thead>
<tr>
<th>Category</th>
<th>Description</th>
<th>Display Order</th>
<th>Selection Type</th>
</tr>
</thead>
<tbody class="procedures">
<%= f.fields_for :procedures do |procedure| %>
<%= render 'procedure_fields', f: procedure %>
<% end %>
</tbody>
</table>
<%= link_to_add_association 'Add Skill', f, :procedures,
data: { association_insertion_node: '.procedures', association_insertion_method: :append } %>
<br><br>
<%= link_to_remove_association "Remove Category", f %>
</div>
Finally, here is my partial for rendering Procedures:
<tr class="nested-fields">
<td><%= f.text_field :skill, class: 'form-control' %></td>
<td><%= f.text_field :description, class: 'form-control' %></td>
<td><%= f.text_field :display_order, class: 'form-control' %></td>
<td><%= link_to_remove_association "Remove Skill", f %></td>
</tr>
Thank you for having a look.
Upvotes: 0
Views: 877
Reputation: 20263
I'm guessing you have .procedures
under each of your Category
sections. In which case, you're appending to both instances of the .procedures
class.
You need to create a unique identifier for each Category
section (e.g., #procedure-category-6
where 6 is assigned dynamically) and then use #procedure-category-6 .procedures
for targeting your insertion.
Upvotes: 2