我正在使用 Cocoon Gem 來構建動態表單。一切正常。我可以添加新記錄,洗掉記錄。但是我的表單和提交按鈕顯示兩次。我已經檢查了application.html.erb布局檔案,但我沒有在我的布局檔案中添加 coccon。
我添加 coccon gem 的方式是gem coccon在 Gemfile 中添加,然后我使用 yarn 添加了 cocconyarn add @nathanvda/cocoon 并在application.js檔案中添加了以下行
require("jquery")
require("@nathanvda/cocoon")
我的表單視圖如下所示(這是顯示兩次的表單)
<%= form_with model: @user do |f| %>
<%= f.fields_for :work_experiences do |work_experience| %>
<div id='work_experiences'>
<%= render "work_experience_fields", f: work_experience %>
</div>
<%= link_to_add_association 'add more work experience', f, :work_experiences, data: { association_insertion_node: '#work_experiences', association_insertion_method: :append } %>
<%= f.submit 'Update Work Experience' %>
<% end %>
<% end %>
我的部分_work_experience_fields如下所示:
<%= f.label :job_tile, 'Job Title' %>
<%= f.text_field :job_title, placeholder: 'Enter Job Title' %>
<%= f.label :company_name, 'Company Name' %>
<%= f.text_field :company_name, placeholder: 'Enter Company Name' %>
<%= link_to_remove_association "remove task", f %>
請幫我除錯為什么我的表單顯示兩次。
uj5u.com熱心網友回復:
您的表單應如下所示: 基本上,您需要關閉fields_for回圈。
<%= form_with model: @user do |f| %>
<div id='work_experiences'>
<%= f.fields_for :work_experiences do |work_experience| %>
<%= render "work_experience_fields", f: work_experience %>
<%end%>
</div>
<%= link_to_add_association 'add more work experience', f,
:work_experiences, data: { association_insertion_node:
'#work_experiences', association_insertion_method: :append } %>
<%= f.submit 'Update Work Experience' %>
<% end %>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/358379.html
