我最近遇到了这个弃用警告
DEPRECATION WARNING: Method size is deprecated and will be removed in Rails 5.1,as
ActionController::Parameters
no longer inherits from hash. Using this deprecated behavior exposes potential security problems. If you continue to use this method you may be creating a security vulnerability in your app that can be exploited.
Params看起来像这样:
<ActionController::Parameters { "objects" => <ActionController::Parameters { "0"=>{"priority"=>"24","style"=>"three_pictures"},"1"=>{"priority"=>"24","2"=>{"priority"=>"24","style"=>"three_pictures"} } permitted: false> } permitted: false>
我试图找到像这样的对象的大小:
ParaMS [:对象] .size
然后我尝试了长度和计数相同的事情,这导致了相同的警告.这将是一个什么样的工作? .keys.length是有效的,但这是正确的方法,还是我错过了什么?
解决方法
正如评论中提到的,你必须将params转换为Hash,因为在Rails 5中,params不再继承Hash.所以.size,.length和.count不能直接用于params.
如何将其转换为Hash(可能更短的代码):
permitted_params = params.require(:your_model_name).permit( :product_inspirationals => [ :priority,:style ] ).to_h puts permitted_params[:product_inspirationals].length
不了解您的模型结构,因此您必须根据需要进行调整.