我目前有一个表单,我需要在保存之前从控制器中的参数中删除一些字段(字段没有列,它们仅用于填充链接选择)。
问题是这样的:在我有机会删除它们之前,我收到了不应发送的字段的批量分配错误。我的理解是,错误只会在控制器到达时触发,@sale = Sale.new(params[:sale])
但它似乎在此之前发生(我在下面控制器的最开始放置了 line,puts "sale params: " + params[:sale].to_s
但它不会触发,只会发生质量分配错误。
我的控制器创建操作代码如下所示:
def create
puts "sale params: " + params[:sale].to_s
params[:sale] = params[:sale].except([:vehicles_attributes])
@sale = Sale.new(params[:sale])
if @sale.save
redirect_to @sale, :notice => "Successfully created address."
else
render :action => 'new'
end
end
如何确保在收到批量分配错误之前从参数中删除 :vehicles_attributes 哈希?
编辑:完全错误,根据要求:
ActiveModel::MassAssignmentSecurity::Error in SalesController#create
Can't mass-assign protected attributes: make, model
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"LX/uEp5o9smc3sCcjQbOXQQgaK2wihvS0OPZTdNOj6w=",
"sale"=>{
"sale_type_id"=>"1",
"date"=>"",
"customer_attributes"=>{
"customer_type_id"=>"1",
"emails_attributes"=>{
"0"=>{
"value"=>""}}},
"vehicles_attributes"=>{
"0"=>{
"make"=>"4",
"model"=>"38",
"trim_id"=>"658",
"model_year_id"=>"12"
}}}}
Make 和 Model 是我正在尝试删除的属性params[:sale] = params[:sale].except([:vehicles_attributes])
(它们位于 :vehicles_attributes 哈希中,正如您从错误中看到的那样)。