stimulus-rails-nested-form
stimulus-rails-nested-form copied to clipboard
Avoid duplicating fields (maybe not an Issue)
Hi,
I just introduced this complenent to my pet project and found that it works well, except for the fact that I can't find a way to avoid creating duplicate records when adding more nested fields - I'm using it to select associated skills to training drills.
Form:
<%= javascript_pack_tag "application" %>
<%= form_with model: drill, data: { controller: 'nested-form', nested_form_wrapper_selector_value: '.nested-form-wrapper' } do |form| %>
<template data-nested-form-target="template">
<%= form.fields_for :skills, Drill.new, child_index: 'NEW_RECORD' do |skill_fields| %>
<%= render 'skill_form', form: skill_fields %>
<% end %>
</template>
<%= form.fields_for :skills do |skill_fields| %>
<%= render 'skill_form', form: skill_fields %>
<% end %>
<div data-nested-form-target="target"></div>
<button type="button" data-action="nested-form#add">Add</button>
(SKILL PARTIAL)
<div class='nested-fields'>
<div class='form-group'>
<%= form.hidden_field :_destroy %>
<%= form.text_field :name, placeholder: 'Fundamento', class: 'form-control', size: 10 %>
<button style="height:32px;width:32px" type="button" data-action="nested-form#remove"><%= image_tag "minus.png" %></button>
</div>
</div>