Rails 4 深度嵌套表单 Cocoon 不起作用
Rails 4 Deep Nested Form with Cocoon Not Working
我正在尝试使用 Cocoon 在 Rails 4
中深度嵌套动态表单
我在第 2 级嵌套表单 (_milling_datum_fields) 的 link_to_remove_association 函数上遇到错误。
错误:未定义的方法“new_record?” 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
铣削基准
class MillingDatum < ActiveRecord::Base
belongs_to :setup
has_many :notes, as: :notable
我的观点
projects/_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>
...
layouts/_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>
layouts/_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
我在互联网上搜索了类似的主题,但我无法弄清楚。
我开始认为我的模型有问题。
如果我 post 我的 projects_controller.rb
会有帮助吗?
感谢您的帮助。
好的伙计们,我解决了我的特殊问题。
问题是我在父对象控制器 (projects_controller) 中定义的嵌套强参数未根据模型中的关系定义正确复数化。
给我错误:错误:未定义的方法`new_record? nil: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
根据模型中定义的关系,包含嵌套的强参数和每个对象的正确复数。
让您的父模型根据模型中定义的关系定义正确的 accepts_nested_attributes_for 以及每个对象的正确复数形式。
使用正确的partials结构,html,coccoon根据cocoon说明提供功能。
我正在尝试使用 Cocoon 在 Rails 4
中深度嵌套动态表单我在第 2 级嵌套表单 (_milling_datum_fields) 的 link_to_remove_association 函数上遇到错误。
错误:未定义的方法“new_record?” 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
铣削基准
class MillingDatum < ActiveRecord::Base
belongs_to :setup
has_many :notes, as: :notable
我的观点
projects/_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>
...
layouts/_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>
layouts/_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
我在互联网上搜索了类似的主题,但我无法弄清楚。
我开始认为我的模型有问题。
如果我 post 我的 projects_controller.rb
会有帮助吗?
感谢您的帮助。
好的伙计们,我解决了我的特殊问题。
问题是我在父对象控制器 (projects_controller) 中定义的嵌套强参数未根据模型中的关系定义正确复数化。
给我错误:错误:未定义的方法`new_record? nil: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
根据模型中定义的关系,包含嵌套的强参数和每个对象的正确复数。
让您的父模型根据模型中定义的关系定义正确的 accepts_nested_attributes_for 以及每个对象的正确复数形式。
使用正确的partials结构,html,coccoon根据cocoon说明提供功能。