【问题标题】:Cocoon Gem is rendering form twiceCocoon Gem 两次渲染表单
【发布时间】:2021-11-12 06:59:52
【问题描述】:

我正在使用 Cocoon Gem 构建动态表单。每件事都运行良好。我可以添加新记录,删除记录。但是我的表单和提交按钮显示了两次。我检查了application.html.erb 布局文件,但我的布局文件中没有添加 coccon。

我添加 coccon gem 的方式是在 Gemfile 中添加 gem coccon,然后使用 yarn yarn add @nathanvda/cocoon 添加 coccon 并在 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 %>

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

【问题讨论】:

    标签: ruby-on-rails cocoon-gem


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-04
      • 1970-01-01
      • 2018-01-28
      • 2023-04-04
      • 2016-07-03
      • 1970-01-01
      相关资源
      最近更新 更多