gpt4 book ai didi

ruby-on-rails - Rails3 中的 before_create、before_update、before_save、before_destroy 弃用警告

转载 作者:行者123 更新时间:2023-12-04 05:11:45 25 4
gpt4 key购买 nike

我刚刚将我的应用程序从 Rails 2.3 升级到 3,我得到了一些
我的 before_create、update、save、destroy 等的弃用警告。

有谁知道如何解决这个问题?

这些是我的警告:

DEPRECATION WARNING: Base#before_create has been deprecated, please use Base.before_create :method instead. (called from /Users/macmini/qna/app/models/user.rb:32)
DEPRECATION WARNING: Base#before_update has been deprecated, please use Base.before_update :method instead. (called from /Users/macmini/qna/app/models/user.rb:40)
DEPRECATION WARNING: Base#after_save has been deprecated, please use Base.after_save :method instead. (called from /Users/macmini/qna/app/models/user.rb:50)
DEPRECATION WARNING: Base#before_destroy has been deprecated, please use Base.before_destroy :method instead. (called from /Users/macmini/qna/app/models/user.rb:56)

只是 before_create 的一个例子:
  def before_create
self.username.downcase!
self.salt = User.make_salt(self.username)
self.hashed_password = User.hash_with_salt(@password, self.salt)
end

最佳答案

您看到的警告是 Rails 3 试图阻止您覆盖基础 before_*after_*方法。这与您拥有 before_filter 的方式类似和 Controller 中的其他回调。

这意味着,而不是做:

def before_create
self.username.downcase!
self.salt = User.make_salt(self.username)
self.hashed_password = User.hash_with_salt(@password, self.salt)
end

Rails 希望你这样做:
before_create :downcase_username_and_create_password

def downcase_username_and_create_password
self.username.downcase!
self.salt = User.make_salt(self.username)
self.hashed_password = User.hash_with_salt(@password, self.salt)
end

在这种情况下,您甚至可以将两者分开,因为您可能希望独立生成密码:
before_create :downcase_username, :create_password

def downcase_username
self.username.downcase!
end

def create_password
self.salt = User.make_salt(self.username)
self.hashed_password = User.hash_with_salt(@password, self.salt)
end

关于ruby-on-rails - Rails3 中的 before_create、before_update、before_save、before_destroy 弃用警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3766136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com