Rails cocoon nested form only receiving one first nested attribute

社会主义新天地 提交于 2019-12-06 05:32:47

You need to match exactly the name of the association within fields_for. Change this:

<%= f.fields_for :substep do |ff| %>

To

<%= f.fields_for :substeps do |ff| %>

EDIT: Cocoon expects a strict DOM structure to work, try changing

<%= f.fields_for :substep do |ff| %>
  <%= render "substep_fields", :f => ff %>
<% end %>
<%= link_to_add_association 'Add substep', f, :substeps %>

To:

<div id="substeps">
  <%= f.fields_for :substep do |ff| %>
    <%= render "substep_fields", :f => ff %>
  <% end %>
  <div class="links">
    <%= link_to_add_association 'Add substep', f, :substeps %>
  </div>
</div>

If I remember correctly, only wrapper around link_to_add_association is required, the most top-level wrapper is just nice to have.

There are two changes you need to make. One is as BroiSatse wrote, changing

<%= f.fields_for :substep do |ff| %>

to

<%= f.fields_for :substeps do |ff| %>

and second, changing

params.require(:step).permit(..., substeps_attributes: [:id, :description, :action, :_destroy])

to

params.require(:step).permit(..., substeps_attributes: [[:id, :description, :action, :_destroy]])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!