嵌套表单未保存

时间:2017-03-13 23:40:01

标签: ruby-on-rails ruby-on-rails-5 simple-form cocoon-gem

我使用宝石Cocoon作为嵌套表格,这个表格没有提交...... 我做错了什么?谢谢你

_form.html.erb

<div class="container">
  <div class="row">
    <%= simple_form_for @event do |f| %>
    <div class="col-xs-12">
      <h3>Your event to share</h3>
      <%= f.input :name, label: "Event's name" %>
      <%= f.input :total_price, label: "What is the total price" %>
      <h3> Add your friends to share the bill</h3>
      <div id="participants">
        <%= f.simple_fields_for :participants do |participant| %>
          <%= render "participants_fields", f: participant %>
        <% end %>
        <div class="links">
          <%= link_to_add_association "add a friend", f, :participants, partial: "participants_fields", class:"btn btn-primary" %>
        </div>
      </div>
        <%= f.button :submit %>
    </div>
  <% end %>
  </div>
</div>

_participants_fields.html.erb

<div class="nested-fields">
  <%= f.input :first_name, label: "Enter your friend's first name" %>
  <%= f.input :salary, label: "Enter his/her monthly pay" %>
  <div class="links">
    <%= link_to_remove_association "Remove this friend", f , class: "btn btn-danger btn-xs" %>
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

我使用本教程几小时前用cocoon gem实现嵌套表单。你可能会发现它很有用。 https://www.youtube.com/watch?v=56xjUOAAZY8

要深入了解如何创建嵌套表单,您还应该看到有关如何在没有gem的情况下执行此操作的视频。 https://www.youtube.com/watch?v=pulzZxPkgmE

答案 1 :(得分:0)

我错过了这种关系    has_many :participants, inverse_of: :event

我添加了has_many:参与者,依赖:: destroy