2016-01-12 92 views
2

林欲借繭深深窩動態形式的Rails 4軌道4深層嵌套形式蠶繭不工作

我可是從第二級嵌套形式(_milling_datum_fields)才能上link_to_remove_association功能的錯誤。

錯誤:未定義的方法`new_record?' for nil: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

projects_controller強PARAMS

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會有幫助嗎? 感謝您的幫助。

+0

我在setup下提取了雙嵌套的milling_datum成它自己的形式,但仍然有同樣的問題。我將'has_one:milling_datum'的設置關聯改爲'has_many:milling_data',它的工作原理是 – JerryA

+0

這與我如何完成我的強大參數有關。 has_one belongs_to協會將有單數命名強參數。 'milling_data_attributes'到'milling_datum_attributes',這是單數版本。 – JerryA

+0

在你的projects_controller中,你是爲每一個構建project.setups和setup.milling_data? –

回答

2

好吧夥計們我想出了我的特殊問題。

問題是我的父對象控制器(projects_controller)中定義的嵌套強參數沒有根據我的模型中的關係定義正確複用。

給我的錯誤:錯誤:未定義的方法`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寶石

  1. 包含嵌套的強參數和正確的多元化e根據模型中定義的關係來確定一個對象。

  2. 根據模型中定義的關係,讓您的父模型根據每個對象的正確複數定義正確的acceptenesnes_attributes_for。

  3. 使用partials,html和coccoon的正確結構根據cocoon指令提供函數。