Cocoon Gem 两次渲染表单

Cocoon Gem is rendering form twice

我正在使用 Cocoon Gem 构建动态表单。一切正常。我能够添加新记录,删除记录。但是我的表单和提交按钮显示了两次。我已经检查了 application.html.erb 布局文件,但我没有在我的布局文件中添加 coccon。

我添加 coccon gem 的方式是在 Gem 文件中添加 gem coccon 然后我使用 yarn yarn add @nathanvda/cocoon 添加 coccon 并在 [=17] 中添加以下行=] 文件

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 %>

请帮我调试为什么我的表单显示两次。

您的表单应如下所示:基本上,您需要关闭 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 %>