ActionController::Parameters deprecation warning: Method size is deprecated and will be removed in Rails 5.1

Sambhav Sharma

I recently came across this deprecation warning

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 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 like this: params[:objects].size

And then I tried the same thing with length and count, which results in the same warning. What would be the work around for this? .keys.length is something that works, but is this the correct way to do it or am I missing something here?

Sven R.

As mentioned in the comments you have to convert params to Hash since in Rails 5 params no longer inherits from Hash. So .size, .length and .count won't work on params directly.

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

Don't know about your model structure so you have to adjust it to your needs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

DEPRECATION WARNING: Method length is deprecated and will be removed in Rails 5.1

How do I resolve the deprecation warning "Method to_hash is deprecated and will be removed in Rails 5.1"

Getting rid of DEPRECATION WARNING: env is deprecated and will be removed from Rails 5.0.

DEPRECATION WARNING: before_filter is deprecated and will be removed in Rails 5.1. Use before_action instead

DEPRECATION WARNING: alias_method_chain is deprecated

Rails 5 deprecation warning for tests

DEPRECATION WARNING: update is deprecated and will be removed from Rails 6.1 (please, use update instead) (called from block in set_order_id at

Dangerous query method deprecation warning on Rails 5.2.3

Rails 4.2 DEPRECATION WARNING: `serialized_attributes` is deprecated without replacement,

DEPRECATION WARNING: called deprecated method `[]' of an Aws::Resources::Collection

undefined method `map' for #<ActionController::Parameters> Rails 5.1

DEPRECATION WARNING: ActionController::TestCase HTTP request methods will accept only keyword arguments in future Rails versions

DEPRECATION WARNING in rails 4

Rails 5 Deprecation Warning "The behavior of `attribute_change`"

DEPRECATION WARNING: Model.scoped is deprecated

Rails ActionController parameters Error

Rails - ActionController::Parameters to string

Rails4 Deprecation Warning

Deprecation warning for using (*args) with method

ActionController::UnfilteredParameters (unable to convert unpermitted parameters to hash) - Rails 5

Can "!ruby/hash:ActionController::Parameters" be removed when object.to_yaml (YAML) in ruby on rails?

Why do we have deprecated methods or class in use. How do we decide the time frame of deprecation method/class to be completely removed?

ActionController::InvalidAuthenticityToken in Rails 5

ActionController::UnknownFormat in Rails 5

Pandas 2.1.0 warning ''The 'method' keyword in Series.replace is deprecated and will be removed in a future version''

WARNING:tensorflow with constraint is deprecated and will be removed in a future version

Rails 5: DEPRECATION WARNING: You are passing an instance of ActiveRecord::Base to `exists?`. ..I have no idea

How to fix a Deprecation Warning, Firefox ProfilesIni has been deprecated in java

RSpec deprecation warning: The implicit block expectation syntax is deprecated