林欲借茧深深窝动态形式的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? –