ActionController :: Parameter Warning Warning: Method size is outdated and will be removed in Rails 5.1

I recently met this failure warning

DEPARTMENT WARNING: The method size is outdated and will be removed in Rails 5.1, because ActionController::Parameters no longer inherited from the hash. Using this obsolete behavior creates potential security issues. If you continue to use this method, you can create a security vulnerability in your application that could be used.

Params looked like this:

 <ActionController::Parameters { "objects" => <ActionController::Parameters { "0"=>{"priority"=>"24", "style"=>"three_pictures"}, "1"=>{"priority"=>"24", "style"=>"three_pictures"}, "2"=>{"priority"=>"24", "style"=>"three_pictures"} } permitted: false> } permitted: false> 

And I tried to find the size of objects as follows: params[:objects].size

And then I tried the same with length and count , which leads to the same warning. What is the job for this? .keys.length is what works, but is it the right way to do this, or am I missing something here?

+5
source share
2 answers

As mentioned in the comments, you must convert params to Hash since in Rails 5 params no longer inherited from Hash . Therefore, .size , .length and .count will not work directly with parameters.

How to convert it to Hash (shorter code may be possible):

 permitted_params = params.require(:your_model_name).permit( :product_inspirationals => [ :priority, :style ] ).to_h puts permitted_params[:product_inspirationals].length 

I don’t know about your model structure, so you need to customize it to your needs.

+3
source

For the hash, you can find the size using the .size method.

The problem is not with the size method here, the problem is with ActionController::Parameters , which is not a hash,


Look at the first line inside ActionController::Parameters

 "0"=>{priority"=>"24", "style"=>"three_pictures"} 

it must follow " missing before priority

 "0"=>{"priority"=>"24", "style"=>"three_pictures"} 

After that .size method should work

0
source

Source: https://habr.com/ru/post/1258801/


All Articles