実現したいこと
cocoonを使って製品のフォームを複数行追加し、
追加した「各行」に販売国のフォームを複数追加したい。
前提
Ruby on Railsを使ってWEBアプリケーションを開発中です。
モデルは以下の前提となります。
information(1) - (多)product(1) - (多)country_product(多) - (1)country
※countryは事前に登録済みとなります。
informationモデルのnewテンプレート上で、
cocoonを使って複数のproductモデルと複数のcontry_productモデル(におけるcontry_id)の登録を行います。
モデルの登録自体は問題なくできているのですが、
2行目以降の製品に対して販売国を追加すると
1行目の製品の行に販売国フォームが表示されてしまうため困っております。
(あくまでも表示上の問題になります。)
どなたか解決方法がおわかりになる方がいらっしゃいましたらご教授いただければ幸いです。
発生している問題・エラーメッセージ

ブラウザで表示されるhtmlの一部
<tbody class="nested-fields"> <tr> <td></td> <td></td> <td></td> <td class="nested-fields"></td> <td hidden id="country-association-insertion-point"></td> <td class="nested-fields"></td> #2行目で販売国追加をしてもここに追加されてしまう。 <td class="nested-fields"></td> </tr> </tbody> <tbody class="nested-fields"> <tr> <td></td> <td></td> <td></td> <td class="nested-fields"></td> <td hidden id="country-association-insertion-point"></td> <= #2行目で販売国追加をしたらここに追加したい。 </tr> </tbody>
該当のソースコード
new.html.erb
<%= form_with(model: information, local: true) do |f| %> #informationモデルの入力フォームや本件に関係ないコードは割愛 <%= link_to_add_association "製品追加", f, :products, data: { association_insertion_node: '#detail-association-insertion-point', association_insertion_method: 'after' } %> <h> <span>製品情報</span> </h1> <table> <%= f.fields_for :products do |p| %> <%= render partial: "product_fields", locals: { f: p } %> <% end %> <tbody id="detail-association-insertion-point"></tbody> </table> <% end %>
_product_fields.html.erb:
<tbody class="nested-fields"> <tr> <td> <%= link_to_remove_association "削除", f %> </td> <td> <%= link_to_add_association "販売国追加", f, :country_products, data: { association_insertion_node: '#country-association-insertion-point', association_insertion_method: 'after' } %> </td> <td> <%= f.text_field :name%> </td> <%= f.fields_for :country_products do |cp| %> <%= render partial: "country_product_fields", locals: { f: cp } %> <% end %> <td hidden id="country-association-insertion-point"></td> </tr> </tbody>
_country_product_fields.html.erb:
<td class="nested-fields"> <%= f.collection_select :country_id, Country.all, :id, :country, { include_blank: "選択して下さい" } %> <%= link_to_remove_association "削除", f %> </td>

0 コメント