未定义的方法“make_activation_code”使用 Restful_Authentication 时出现 Rails 错误

发布于 2024-08-03 13:18:50 字数 2634 浏览 4 评论 0原文

我有一个去年运行的 Fantasy Football League Rails 应用程序,现在是时候在赛季开始前再次运行它了。我清除了数据库并执行了“rake db:migrate”,这样我就可以从头开始重新启动应用程序。登录页面显示正常,但是当用户尝试使用restful_authentication“注册”时,我在 log/production.log 中收到以下错误:

NoMethodError (undefined method `make_activation_code' for #<User:0xb7743490>):
/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:256:in `method_missing'
/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/callbacks.rb:173:in `send'
/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/callbacks.rb:173:in `evaluate_method'
/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/callbacks.rb:161:in `call'

以下是我的 user.rb 类中的一些片段:

require 'digest/sha1'
require 'gravtastic'

class User < ActiveRecord::Base
  include Authentication
  include Authentication::ByPassword
  include Authentication::ByCookieToken

# has_one :division
has_and_belongs_to_many :divisions

has_gravatar

validates_presence_of     :login
validates_length_of       :login,    :within => 3..40
validates_uniqueness_of   :login,    :case_sensitive => false
validates_format_of       :login,    :with => RE_LOGIN_OK, :message => MSG_LOGIN_BAD

validates_presence_of     :team_name
validates_length_of       :team_name,    :within => 3..40
validates_uniqueness_of   :team_name,    :case_sensitive => false

# validates_format_of       :name,     :with => RE_NAME_OK,  :message => MSG_NAME_BAD,      :allow_nil => true
# validates_length_of       :name,     :maximum => 100

validates_presence_of     :email
validates_length_of       :email,    :within => 6..100 #[email protected]
validates_uniqueness_of   :email,    :case_sensitive => false
validates_format_of       :email,    :with => RE_EMAIL_OK, :message => MSG_EMAIL_BAD

before_create :make_activation_code

# HACK HACK HACK -- how to do attr_accessible from here?
# prevents a user from submitting a crafted form that bypasses activation
# anything else you want your user to change should be added here.
attr_accessible :login, :email, :team_name, :password, :password_confirmation

我的 user.rb 的底部:

protected

def make_activation_code
    self.activation_code = self.class.make_token
end

def make_password_reset_code
  self.reset_password_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
end

make_activation_code是在 User 类中定义的,activation_code 是在迁移中创建的,所以我不明白为什么它未定义。

I have a fantasy football league rails app that was working last year and it's time to get it going again before the season starts. I cleared out the database and did a "rake db:migrate" so I could restart the app from scratch. The login page comes up fine but when a user tries to "sign up" using restful_authentication I get the following error in log/production.log:

NoMethodError (undefined method `make_activation_code' for #<User:0xb7743490>):
/vendor/rails/activerecord/lib/active_record/attribute_methods.rb:256:in `method_missing'
/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/callbacks.rb:173:in `send'
/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/callbacks.rb:173:in `evaluate_method'
/vendor/rails/activerecord/lib/../../activesupport/lib/active_support/callbacks.rb:161:in `call'

Here are some snippets from my user.rb class:

require 'digest/sha1'
require 'gravtastic'

class User < ActiveRecord::Base
  include Authentication
  include Authentication::ByPassword
  include Authentication::ByCookieToken

# has_one :division
has_and_belongs_to_many :divisions

has_gravatar

validates_presence_of     :login
validates_length_of       :login,    :within => 3..40
validates_uniqueness_of   :login,    :case_sensitive => false
validates_format_of       :login,    :with => RE_LOGIN_OK, :message => MSG_LOGIN_BAD

validates_presence_of     :team_name
validates_length_of       :team_name,    :within => 3..40
validates_uniqueness_of   :team_name,    :case_sensitive => false

# validates_format_of       :name,     :with => RE_NAME_OK,  :message => MSG_NAME_BAD,      :allow_nil => true
# validates_length_of       :name,     :maximum => 100

validates_presence_of     :email
validates_length_of       :email,    :within => 6..100 #[email protected]
validates_uniqueness_of   :email,    :case_sensitive => false
validates_format_of       :email,    :with => RE_EMAIL_OK, :message => MSG_EMAIL_BAD

before_create :make_activation_code

# HACK HACK HACK -- how to do attr_accessible from here?
# prevents a user from submitting a crafted form that bypasses activation
# anything else you want your user to change should be added here.
attr_accessible :login, :email, :team_name, :password, :password_confirmation

bottom of my user.rb:

protected

def make_activation_code
    self.activation_code = self.class.make_token
end

def make_password_reset_code
  self.reset_password_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
end

The make_activation_code is defined in the User class and activation_code was created in the migration so I don't understand why is it undefined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

像极了他 2024-08-10 13:18:50

我无法直接说明如何解决这个问题,但在出现异常行为的情况下,我的方法通常是尝试隔离导致问题的原因。在你的情况下,我会尝试创建一个与“make_activation_code”不同名称的方法,看看是否可以将其添加到 before_create 并调用它。如果是这样,请在方法内添加当前在 make_activation_code 中的代码,看看它是否仍然有效。

我见过的与这个特定问题最接近的现象是 Savage Beast 插件,该插件本身有一个 User 模型,可以在应用程序内重新定义 User 模型。这就是为什么看看是否可以在 before_create 中添加不同的方法并查看它是否被调用是很有趣的,这样您就可以验证您的 User 模型本身不会以某种方式被其他部分中定义的流氓 User 模型替换您的应用程序的。

测试这一理论的另一种方法是看看它在生产模式中的工作方式是否与在开发模式中的工作方式不同。在生产中,模型不会在请求之间重新加载,因此插件中的一个模型/方法在初始环境加载后覆盖另一个模型/方法的问题不太可能出现。

I couldn't speak to how to solve this problem directly, but in situations where anomalous behavior is at hand, my approach is usually to try to isolate what's causing the problem. In your case, I'd try things like creating a method of a different name than "make_activation_code," see if you can add it to before_create and it gets called. If so, add the code that's currently in make_activation_code inside the method and see if it still works.

The closest phenomenon I have seen to this specific problem is with the Savage Beast plugin, where the plugin itself has a User model which can redefine the User model inside the application. That's why it would be interesting to see if you can add a different method to your before_create and see if it gets called, so you can verify that your User model itself isn't somehow being replaced by a rogue User model defined in some other part of your app.

Another way to test this theory would be to see if it works differently in production than in development mode. In production, the model doesn't get reloaded between requests, so there is less likely to be problems with one model/method in a plugin overriding another after the initial environment load.

无力看清 2024-08-10 13:18:50

您是否尝试过对受保护的线路进行注释?

Did you try with out-commenting the protected-line?

感性不性感 2024-08-10 13:18:50

好的,我找到了问题的答案。我必须将 before_create 更改为如下所示:

  def before_create
    self.activation_code = self.class.make_token
  end

  def make_password_reset_code
    self.reset_password_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
  end

必须是 Rails 中的内部更改。

Ok I found the answer to my question. I had to change around the before_create to look like this:

  def before_create
    self.activation_code = self.class.make_token
  end

  def make_password_reset_code
    self.reset_password_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join )
  end

Must have been an internal change in Rails.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文