我正在使用rails 4和嵌套表单以及强参数。
我需要从一个表单更新多个模型。
这就是我宣布强大参数的方式。从父控制器。 关联是has_many和belongs_to按此顺序运行Rundatum-> Material-> ParticleSize
def rundatum_params
params.require(:rundatum).permit( :material, :company_id, :material_density, :feed_moisture, :date, :building, :machine, :material_weight, :time_mins, :rate_lb_hr, :mill_amps, :class_amps, :mill_liner, :beater_plate_size, :mill_rpm, :class_rpm, :feeder_type, :feeder_setting, :feeder_aug_diameter, :tlgs_set, :air_pressure, :temp_mill_out, :temp_prod_out, :temp_ambient,
materials_attributes: [:id, :name, :density, :msds_url, :moisture, :notes, :_destroy],
particle_sizes_attributes: [:id, :screen, :percent_through, :percent_retained, :_destroy])
end
rails服务器的输出是:
Rundatum Load (0.6ms) SELECT "rundata".* FROM "rundata" WHERE "rundata"."id" = $1 LIMIT 1 [["id", 7]]
Unpermitted parameters: particle_sizes_attributes
从一个表单更新多个模型时,声明强参数的方法是什么?
由于
答案 0 :(得分:5)
如果ParticleSize
嵌套在Material
内,那么您也应该嵌套参数。
def rundatum_params
params.require(:rundatum).permit( :material, :company_id, :material_density, :feed_moisture, :date, :building, :machine, :material_weight, :time_mins, :rate_lb_hr, :mill_amps, :class_amps, :mill_liner, :beater_plate_size, :mill_rpm, :class_rpm, :feeder_type, :feeder_setting, :feeder_aug_diameter, :tlgs_set, :air_pressure, :temp_mill_out, :temp_prod_out, :temp_ambient,
{ materials_attributes: [:id, :name, :density, :msds_url, :moisture, :notes, :_destroy,
{ particle_sizes_attributes: [:id, :screen, :percent_through, :percent_retained, :_destroy] }] })
end