I am currently using the cocoon gem to handle my nested form along with bootstrap/simple_form. My models are laid out like this:
- A Contact
has_many
goals - A Goal
has_many
tasks - A Task
has_one
reminder (not yet implemented, this is next once I resolve this issue)
My form is laid out with the Goal fields being built correctly, and I can add/remove tasks to my first Goal without a problem. However, when I dynamically add another Goal (regardless if it's in the new or edit action), I cannot add/remove tasks to it (if I click 'add task', it only gets added to my first Goal). Below are my forms:
_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>
I have gone through my code to make sure that I am using proper div classes and ids, but I'm sure there is a problem with my form somewhere. I also added the necessary wrapper option for simple_form/bootstrap. Thank you in advance for taking the time to helping me out.