次のようなテーブルを作成したく、
tbody内にあるtrを動的に追加、削除する機能を実装したいです。
追加ボタンを押した際は、上手く追加してくれるのですが、
削除ボタンを押すとtbody以下の内容がすべて消えてしまします。
想定する挙動としましては、ボタンを押した箇所のtrタグ内のコードのみ消えてほしいのですが。。。
宜しくお願いいたします。
_form.html.erb
<table class="table"> <thead> <tr> <th class="col-sm-1"><%= t('aaaaa') %></th> <th class="col-sm-1"><%= t('bbbbb') %></th> <th class="col-sm-2"><%= t('ccccc') %></th> <th></th> </tr> </thead> <tbody class="nested-fields"> <%= f.fields_for :categories do |ff| %> <%= render "category_fields", { f: ff } %> <% end %> </tbody> </table> <%= link_to_add_association '追加', f, :categories, data: { association_insertion_node: '.nested-fields', association_insertion_method: 'append' }, class: 'btn' %>
_category_fields.html.erb
<tr> <td class="col-sm-1"><%= f.select aaaaa %></td> <td class="col-sm-1"><%= f,text_field bbbb %></td> <td class="col-sm-2"><%= f.text_field cccc %></td> <td><%= link_to_remove_association '削除', f %></td> </tr>
UserモデルとCategoryモデルがあり、
今回は、Categoryモデルを動的に追加、削除できるようにしたいです。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。