以编程方式为Rails 5.1和更新

时间:2016-06-05 22:06:31

标签: actioncontroller ruby-on-rails-5

我的项目中有几个before_actions,它们会在调用REST操作之前设置/合并一些其他参数。这让我可以用最少的自定义来覆盖一些基本的gems方法。

但是,在执行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.

我查看了文档链接,但找不到任何明显的方法,可以让我以编程方式向params对象添加一个不会触发此警告的参数。

未能找到非哈希方法意味着我的项目将与Rails 5.1 +

不兼容

感谢任何帮助

1 个答案:

答案 0 :(得分:2)

你没有合并!,但你确实有合并。所以你仍然可以在你的控制器中做到:

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

params = params.merge(animal: "Bokito")