title: Rails 4 Deep Nested Form with Cocoon Not Working
body: <p>Im attempting to use Cocoon to deeply nest dynamic forms in Rails 4</p>
  
  <p>Im getting an error on the link_to_remove_association function from the 2nd level nested form (_milling_datum_fields).</p>
  
  <p>ERROR: undefined method `new_record?' for nil:NilClass</p>
  
  <p>Sorry for the mass amount of code.</p>
  
  <p>My models of concern.</p>
  
  <p><strong>Project</strong></p>
  
  <pre><code>class Project &lt; ActiveRecord::Base
    belongs_to :company, inverse_of: :projects
  
    has_many :project_materials
    has_many :materials, through: :project_materials
    has_many :finished_materials, class_name: 'Material', through: :setups
  
    has_many :setups
    has_many :milling_data, through: :setups
    has_many :class_data, through: :setups
    has_many :screen_data, through: :setups
  
    has_many :notes, as: :notable
    has_many :tasks, inverse_of: :project, dependent: :destroy
  
    accepts_nested_attributes_for :tasks, :notes, allow_destroy: true, reject_if: :all_blank
    accepts_nested_attributes_for :materials, :setups, :milling_data, :class_data, :screen_data, :finished_materials, reject_if: :all_blank
  </code></pre>
  
  <p><strong>Setup</strong></p>
  
  <pre><code>class Setup &lt; ActiveRecord::Base
      belongs_to :project
      has_one :finished_material, class_name: "Material"
      has_one :milling_datum
      has_one :class_datum
      has_one :screen_datum
  
      accepts_nested_attributes_for :milling_datum, :class_datum, :screen_datum, :finished_material, reject_if: :all_blank
  end
  </code></pre>
  
  <p><strong>MillingDatum</strong></p>
  
  <pre><code>class MillingDatum &lt; ActiveRecord::Base
      belongs_to :setup
      has_many :notes, as: :notable
  </code></pre>
  
  <p>My Views</p>
  
  <p><strong>projects/_form.html.erb</strong></p>
  
  <pre><code>&lt;%= form_for(@project) do |f| %&gt;
  ...
   &lt;div class="col-lg-6"&gt;
      &lt;%= f.fields_for :setups do |setup| %&gt;
        &lt;%= render 'layouts/setup_fields', f: setup %&gt;
      &lt;% end %&gt; 
      &lt;div class="links float-e-margins"&gt;
        &lt;%= link_to_add_association 'add setup', f, :setups, partial: 'layouts/setup_fields', class: "btn btn-outline btn-default btn-xs" %&gt;
      &lt;/div&gt;    
    &lt;/div&gt;
  ...
  </code></pre>
  
  <p><strong>layouts/_seutp_fields.html.erb</strong></p>
  
  <pre><code>&lt;div class="nested-fields"&gt;
  ...
    &lt;div class="col-lg-6"&gt;
        &lt;%= f.fields_for :milling_data do |md| %&gt;
          &lt;%= render 'layouts/milling_datum_fields', f: md %&gt;
        &lt;% end %&gt; 
        &lt;div class="links float-e-margins"&gt;
          &lt;%= link_to_add_association 'add milling data', f, :milling_data, partial: 'layouts/milling_datum_fields', class: "btn btn-outline btn-default btn-xs" %&gt;
        &lt;/div&gt;    
      &lt;/div&gt;
    &lt;div class="col-lg-6"&gt;
        &lt;%= f.fields_for :class_data do |cd| %&gt;
          &lt;%= render 'layouts/class_datum_fields', f: cd %&gt;
        &lt;% end %&gt; 
        &lt;div class="links float-e-margins"&gt;
          &lt;%= link_to_add_association 'add class data', f, :class_data, partial: 'layouts/class_datum_fields', class: "btn btn-outline btn-default btn-xs" %&gt;
        &lt;/div&gt;    
      &lt;/div&gt;
      &lt;div class="col-lg-6"&gt;
        &lt;%= f.fields_for :screen_data do |sd| %&gt;
          &lt;%= render 'layouts/screen_datum_fields', f: sd %&gt;
        &lt;% end %&gt; 
        &lt;div class="links float-e-margins"&gt;
          &lt;%= link_to_add_association 'add screen data', f, :screen_data, partial: 'layouts/screen_datum_fields', class: "btn btn-outline btn-default btn-xs" %&gt;
        &lt;/div&gt;    
      &lt;/div&gt;
      &lt;div class="col-lg-6"&gt;
        &lt;%= f.fields_for :finished_materials do |fm| %&gt;
          &lt;%= render 'material_fields', f: fm %&gt;
        &lt;% end %&gt; 
        &lt;div class="links float-e-margins"&gt;
          &lt;%= link_to_add_association 'add finished material', f, :finished_materials, partial: 'projects/material_fields', class: "btn btn-outline btn-default btn-xs" %&gt;
        &lt;/div&gt;    
      &lt;/div&gt;
    &lt;/div&gt;
        &lt;%= link_to_remove_association "X", f, class: "btn btn-outline btn-danger btn-xs pull-right" %&gt;
  
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>layouts/_milling_datum_fields.html.erb</strong></p>
  
  <pre><code>&lt;div class="nested-fields"&gt;
  ...
   &lt;div class="field form-group"&gt;
          &lt;%= f.label :rate_test_lbs_hr %&gt;&lt;br&gt;
          &lt;%= f.number_field :rate_test_lbs_hr, class: "form-control" %&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;%= link_to_remove_association "X", f, class: "btn btn-outline btn-danger btn-xs pull-right" %&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p><strong>projects_controller</strong> strong params</p>
  
  <pre><code>def project_params
        params.require(:project).permit(:name, :scheduled_start_date, :estimated_end_date, :employees_needed, :incoming_packaging, :final_product_packaging, :sample_instructions, :project_description, :project_type, :building_id, :paid_status, :material_total_weight_lbs, :shifts, :shift_hrs, :rate_lb_hr, :company_id,
          materials_attributes: [:id, :_destroy, :material_type, :name, :moisture, :density_g_cm3, :msds_url],
          notes_attributes: [:id, :_destroy, :content, :notable_id, :notable_type],
          setups_attributes: [:id, :_destroy, :project_id, 
            milling_data_attributes:[ 
              :id, :_destroy, :date_milled, :mill_model, :mill_liner_type, :mill_tlgs_set, :mill_rpm, :mill_amps, :mill_class_rpm, :mill_class_amps, :feeder_model, :feeder_speed_setting, :feeder_auger_diam_inch, :air_pressure_iw_mill_out, :air_pressure_iw_mill_in, :air_pressure_iw_dustcollector_baghouse, :air_pressure_iw_dustcollector_exit, :air_pressure_iw_blower_out, :temp_ambeint, :temp_mill_out, :temp_prod_out, :rate_test_total_lbs, :rate_test_total_time, :rate_test_lbs_hr, :setup_id
            ],
            screen_data_attributes:[
              :id, :_destroy, :screen_base_pci_id, :top_weight, :bottom_weight, :weight_lead, :deck_count, :screen_1_mesh, :screen_2_mesh, :screen_3_mesh, :setup_id
            ],
            class_data_attributes: [
             :id, :_destroy, :rate_lb_hr, :class_pci_id, :rpm, :secondary_air_setting, :ap_iw_secondary_air_in, :ap_iw_main_exit, :ap_iw_main_entrance, :setup_id
            ],
            finished_materials_attributes: [
              :id, :_destroy, :material_type, :moisture, :name, :msds_url, :density_g_cm3, :setup_id 
            ]
          ])
      end
  </code></pre>
  
  <p>Ive scoured the similar threads on the Internet but I can not figure it out.
  Im starting to think I have something wrong in my models.
  Would it help if I post my <code>projects_controller.rb</code> ?
  Thanks for helping.</p>