林欲借繭深深窩動態形式的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
會有幫助嗎? 感謝您的幫助。
我在setup下提取了雙嵌套的milling_datum成它自己的形式,但仍然有同樣的問題。我將'has_one:milling_datum'的設置關聯改爲'has_many:milling_data',它的工作原理是 – JerryA
這與我如何完成我的強大參數有關。 has_one belongs_to協會將有單數命名強參數。 'milling_data_attributes'到'milling_datum_attributes',這是單數版本。 – JerryA
在你的projects_controller中,你是爲每一個構建project.setups和setup.milling_data? –