Rails 4深层嵌套表单,茧不起作用

时间:2016-01-12 21:03:40

标签: ruby-on-rails-4 cocoon-gem

我试图使用Cocoon在Rails 4中深度嵌套动态表单

我从第二级嵌套表单(_milling_datum_fields)获取link_to_remove_association函数的错误。

错误:未定义的方法`new_record?'为零:NilClass

对于大量的代码感到抱歉。

我的关注模型。

项目

class Project < 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

设置

class Setup < 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

MillingDatum

class MillingDatum < ActiveRecord::Base
    belongs_to :setup
    has_many :notes, as: :notable

我的观点

项目/ _form.html.erb

<%= form_for(@project) do |f| %>
...
 <div class="col-lg-6">
    <%= f.fields_for :setups do |setup| %>
      <%= render 'layouts/setup_fields', f: setup %>
    <% end %> 
    <div class="links float-e-margins">
      <%= link_to_add_association 'add setup', f, :setups, partial: 'layouts/setup_fields', class: "btn btn-outline btn-default btn-xs" %>
    </div>    
  </div>
...

布局/ _seutp_fields.html.erb

<div class="nested-fields">
...
  <div class="col-lg-6">
      <%= f.fields_for :milling_data do |md| %>
        <%= render 'layouts/milling_datum_fields', f: md %>
      <% end %> 
      <div class="links float-e-margins">
        <%= link_to_add_association 'add milling data', f, :milling_data, partial: 'layouts/milling_datum_fields', class: "btn btn-outline btn-default btn-xs" %>
      </div>    
    </div>
  <div class="col-lg-6">
      <%= f.fields_for :class_data do |cd| %>
        <%= render 'layouts/class_datum_fields', f: cd %>
      <% end %> 
      <div class="links float-e-margins">
        <%= link_to_add_association 'add class data', f, :class_data, partial: 'layouts/class_datum_fields', class: "btn btn-outline btn-default btn-xs" %>
      </div>    
    </div>
    <div class="col-lg-6">
      <%= f.fields_for :screen_data do |sd| %>
        <%= render 'layouts/screen_datum_fields', f: sd %>
      <% end %> 
      <div class="links float-e-margins">
        <%= link_to_add_association 'add screen data', f, :screen_data, partial: 'layouts/screen_datum_fields', class: "btn btn-outline btn-default btn-xs" %>
      </div>    
    </div>
    <div class="col-lg-6">
      <%= f.fields_for :finished_materials do |fm| %>
        <%= render 'material_fields', f: fm %>
      <% end %> 
      <div class="links float-e-margins">
        <%= link_to_add_association 'add finished material', f, :finished_materials, partial: 'projects/material_fields', class: "btn btn-outline btn-default btn-xs" %>
      </div>    
    </div>
  </div>
      <%= link_to_remove_association "X", f, class: "btn btn-outline btn-danger btn-xs pull-right" %>

  </div>
</div>

布局/ _milling_datum_fields.html.erb

<div class="nested-fields">
...
 <div class="field form-group">
        <%= f.label :rate_test_lbs_hr %><br>
        <%= f.number_field :rate_test_lbs_hr, class: "form-control" %>
      </div>
    </div>
    <%= link_to_remove_association "X", f, class: "btn btn-outline btn-danger btn-xs pull-right" %>
  </div>

projects_controller 强参数

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
我已经在互联网上搜索了类似的主题,但我无法弄明白。 我开始认为我的模型中有错误。 如果我发布projects_controller.rb会有帮助吗? 谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

好吧,伙计们,我想出了我的特殊问题。

问题是我的父对象控制器(projects_controller)中定义的嵌套强params根据我模型中的关系定义没有正确复数。

给我错误:错误:未定义的方法`new_record?'为零:NilClass

我有

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,
        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
          ])

由于我的关系是Project has_many->Setups has_one->Milling_datum,我需要将milling_data_attributes的复数更改为milling_datum_attributes

从研究这个问题开始,我看到了Coccoon Gem最常见的错误

  1. 根据模型中定义的关系,包含嵌套的强参数和每个对象的正确复数。

  2. 让您的父模型根据模型中定义的关系,使用每个对象的正确复数定义正确的accepts_nested_attributes_for。

  3. 使用partials,html和coccoon的正确结构,根据cocoon说明提供了函数。