Search code examples
ruby-on-railsruby-on-rails-5simple-formcocoon-gem

Nested form isn't saved


I am using the gem Cocoon for nested form and this form don't get submitted... What am I doing wrong please? Thank u

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

Solution

  • I was missing this relation has_many :participants, inverse_of: :event

    I add has_many :participants, dependent: :destroy