[英]to_hash and strong_parameters for Rails 5.1 with an array of items
我有以下参数声明:
def line_item_params
params.require(:line_items).map do |p|
ActionController::Parameters.new(p.to_hash).permit(:quantity, :price, :menu_item_id)
end
end
但出现以下错误:
方法to_hash已被弃用,并将在Rails 5.1中删除,因为
ActionController::Parameters
不再继承自hash。 使用此不建议使用的行为会暴露潜在的安全问题。 如果继续使用此方法,则可能是在您的应用程序中创建了一个可以利用的安全漏洞。 相反,请考虑使用不推荐使用以下已记录方法之一: http : //api.rubyonrails.org/v5.0.1/classes/ActionController/Parameters.html
我只是张贴一系列物品。 应该如何处理?
def line_item_params
# just to raise ActionController::ParameterMissing if key is missing
params.require(:line_items)
# this is the actual whitelist
params.permit(line_items: [:quantity, :price, :menu_item_id])
end
您可以通过将散列选项传递给.permit
并将数组包含嵌套对象的白名单的键,从而将对象数组列入白名单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.