加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – ActionController ::参数弃用警告:不推荐使用

发布时间:2020-12-17 03:18:51 所属栏目:百科 来源:网络整理
导读:我最近遇到了这个弃用警告 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 y
我最近遇到了这个弃用警告

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

不了解您的模型结构,因此您必须根据需要进行调整.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读