我目前正在使用 cocoon gem 和 bootstrap/simple_form 来处理我的嵌套表单。我的模型布局如下:
- A 联系
has_many
目标 - A 目标
has_many
任务 - 任务
has_one
提醒(尚未实现,这是我解决此问题后的下一个)
我的表单布局正确,目标字段构建正确,我可以毫无问题地向我的第一个目标添加/删除任务。但是,当我动态添加另一个目标时(无论它是在新建还是编辑操作中),我无法向其中添加/删除任务(如果我单击“添加任务”,它只会添加到我的第一个目标中)。以下是我的表格:
_form.html.erb
<%= simple_form_for(@contact) do |f| %>
<% if @contact.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@contact.errors.count, "error") %> prohibited this contact from being saved:</h2>
<ul>
<% @contact.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :name %>
<%= f.input :title %>
<%= f.input :company %>
<%= f.input :email %>
<%= f.input :notes %>
<h3>Goals:</h3>
<div id="goals">
<%= f.simple_fields_for(:goals) do |goal| %>
<%= render 'goal_fields', :f => goal %>
<div class="links">
<%= link_to_add_association 'add goal', f, :goals, :render_options => {:wrapper => 'bootstrap' } %>
</div>
<% end %>
</div>
<%= f.submit :submit %>
<% end %>
_goal_fields.html.erb
<div class="nested-fields">
<%= f.input :title %>
<%= f.input :due_date %>
<%= f.input :notes %>
<%= link_to_remove_association "remove goal", f %>
<h4>Tasks:</h4>
<div id="tasks">
<%= f.simple_fields_for(:tasks) do |task| %>
<%= render 'task_fields', :f => task %>
<div class="links">
<%= link_to_add_association 'add task', f, :tasks, :render_options => {:wrapper => 'bootstrap' } %>
</div>
<% end %>
</div>
</div>
task_fields.html.erb
<div class="nested-fields">
<%= f.input :task_type %>
<%= f.input :date_of_task %>
<%= f.input :complete, as: :boolean, checked_value: true, unchecked_value: false %>
<%= link_to_remove_association "remove task", f %>
</div>
我已经检查了我的代码以确保我使用了正确的 div 类和 id,但我确信我的表单在某处存在问题。我还为 simple_form/bootstrap 添加了必要的包装器选项。预先感谢您抽出宝贵时间帮助我。