我当前的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 sort
进行任何排序。这是一个示例,以Foo
或key
的顺序向前和向后对val
的实例进行排序:
class Foo
attr_reader :key, :val
def initialize(key, val)
@key = key
@val = val
end
def to_s
"#{key}: #{val}"
end
end
x = Foo.new('x', 1)
y = Foo.new('y', 2)
array = [x, y]
puts array.sort { |a, b| a.key <=> b.key }
puts array.sort { |a, b| b.key <=> a.key }
puts array.sort { |a, b| a.val <=> b.val }
puts array.sort { |a, b| b.val <=> a.val }
使用Rails 5,ActionController :: Parameter将不再继承自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!之类的方法,除了!,extract!,delete等除外。您可以对其进行详细介绍here。