Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a parameter programatically for Rails 5.1 and newer

I have a couple of before_actions in my project that will set/merge some additional parameters before calling REST actions. This lets me override some basic gems methods with minimal customization.

However, I have been getting this deprecation warning in the server logs when doing params.merge! :

DEPRECATION WARNING: Method merge 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. Instead, consider using one of these documented methods which are not deprecated: http://api.rubyonrails.org/v5.0.0.beta2/classes/ActionController/Parameters.html.

I have looked at the documentation link but can't find any obvious method that will let me add a parameter programatically to the params object that will not trigger this warning.

Failure to find a non-hash method means my project will be incompatible with Rails 5.1+

Any help is appreciated

like image 842
Augusto Samamé Barrientos Avatar asked Dec 06 '25 21:12

Augusto Samamé Barrientos


1 Answers

You don't have merge!, but you do have merge. So you should still be able to do in your controller:

params = ActionController::Parameters.new({
         zoo: 'Blijdorp',
         cage: 'monkeys'
     })

params = params.merge(animal: "Bokito")
like image 188
zjeraar Avatar answered Dec 09 '25 21:12

zjeraar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!