2

我有一个带有登录身份验证的 Rails 应用程序,我从 Micheal Hartl 的 RoR 教程中获得。登录有一个记住我的 cookie 登录。问题是当用户cookie存在并且我启动我的rails应用程序时,我收到了这个错误

Processing by PagesController#index as HTML
User Load (1.0ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 37 LIMIT 1
Completed 500 Internal Server Error in 6ms (ActiveRecord: 1.0ms)
NameError (uninitialized constant User::BCRYPT):
app/models/user.rb:30:in `authenticated?'
app/helpers/sessions_helper.rb:12:in `current_user'
app/helpers/sessions_helper.rb:20:in `logged_in?'
app/controllers/pages_controller.rb:5:in `index'

在没有记住我的情况下登录可以正常工作。我已经尝试过我发现的解决方案,例如在 gemfile 或用户模型中放置 require 标记。我什至将我的 ruby​​ 从 2.2.3 降级到 2.1.7 看看它是否会修复,但它没有。

这是我的用户模型

require 'bcrypt'

class User < ActiveRecord::Base
  attr_accessor :remember_token
  has_many :crates, dependent: :destroy
  before_save { self.email = email.downcase }
  validates :alias, presence:true, length: {maximum: 50}, uniqueness:{case_sensitive: true}
  validates :email, presence:true, length:{maximum: 255}, uniqueness:{case_sensitive: false}#,format:{with: VALID_EMAIL_REGEX}
  has_secure_password
  validates :password, presence: true, length: {minimum: 6}, allow_nil: true

def self.digest(string)
  cost = ActiveModel::SecurePassword.min_cost ? BCRYPT::ENGINE::MIN_COST : BCRYPT::ENGINE.cost
  BCRYPT::PASSWORD.create(string, cost: cost)
end

def User.new_token
  SecureRandom.urlsafe_base64
end

# Remembers a user in the database for use in persistent sessions.
def remember
  self.remember_token = User.new_token
  update_attribute(:remember_digest, self.remember_token)
end

# Returns true if the given token matches the digest.
def authenticated?(remember_token)
  return false if remember_digest.nil?
  BCRYPT::PASSWORD.new(remember_digest).is_password?(remember_token)
end

# Forgets a user (cookies)
def forget
  update_attribute(:remember_digest, nil)
end

结尾

4

1 回答 1

2

你的拼写有误。正确的形式应该是BCrypt

例如 :

BCrypt::Password.create(string, cost: cost)

我希望这有帮助 。

于 2015-11-23T09:29:15.867 回答