简体   繁体   English

Rails 4不允许的参数

[英]Rails 4 Unpermitted parameters

I have the following dynamic params depending on the line items i am trying to add to an order 根据要添加到订单中的订单项,我有以下动态参数

{"line_item" => {"items"=>{"0"=>{"price"=>"5.75", "name"=>"Item name", "quantity"=>"5"}, "1"=>{"price"=>"3.35", "name"=>"Item name", "quantity"=>"1"}}}

In my controller: 在我的控制器中:

def lineitems_params
  params.require(:line_item).permit(:key1, :key2, :key3, :key4, :payment_type, :payment_provider).tap do |whitelisted|
    whitelisted[:items] = params[:line_item][:items]
  end
end

I still get the 我仍然得到

Unpermitted parameters: items

in my logs, and it does not update the items. 在我的日志中,它不会更新项目。 How can i solve this? 我该如何解决?

NOTE: the items hash can have many elements inside. 注意:项目哈希可以在其中包含许多元素。

EDIT: 编辑:

In my model: 在我的模型中:

serialize :items, Hash

This should work 这应该工作

def lineitems_params

params.require(:line_item).permit(:key1, :key2, :key3, :key4, :payment_type, :payment_provider, {:items => {:price, :name, :quantity}})

end

Update 更新资料

may be you should just give like this 也许你应该这样给

def lineitems_params
  params.require(:line_item).tap do |whitelisted|
    whitelisted[:items] = params[:line_item][:items]
  end
end

Source 资源

Note: Don't give params.require(:line_items).permit! 注意:不要给params.require(:line_items).permit! it permits all attributes. 它允许所有属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM