ruby-on-rails – ActionController ::参数弃用警告:不推荐使用方法大小,将在Rails 5.1中删除

我最近遇到了这个弃用警告

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

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

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...