首页 > 解决方案 > 方法 sort_by 已弃用 - 可能的解决方案?

问题描述

我目前的 Rails 项目发出警告 -

DEPRECATION WARNING: Method sort_by is deprecated and will be removed in Rails 5.1, as ActionController::Parameters no longer inherits from hash.

我已经搜索了这个警告以及潜在的修复,但根本找不到任何关于它的东西。我想知道这里是否有人有任何建议、修复或替换sort_by?如果这是一个简单的解决方案,我很抱歉,因为我仍在学习 rails。

感谢您花时间阅读和/或回答。

Rails -v 5.0.7.2 | Ruby -v 2.6.4

标签: ruby-on-railsrubyruby-on-rails-5deprecation-warning

解决方案


在 Rails 5 中,ActionController::Parameters 将不再继承自 HashWithIndifferentAccess。

Inheriting from `HashWithIndifferentAccess` allowed users to call any
enumerable methods on `Parameters` object, resulting in a risk of losing the
`permitted?` status or even getting back a pure `Hash` object instead of
a `Parameters` object with proper sanitization.

看看这个变化

如果您需要将 ActionController::Parameters 转换为真正的哈希,那么它支持to_h方法。此外,ActionController::Parameters 将继续拥有诸如 fetch、slice、slice!、except、except!、extract!、delete 等方法。您可以在此处详细了解它们。


推荐阅读