rails 4.2.4未初始化的常量用户:: BCRYPT

时间:2015-11-23 09:16:33

标签: ruby-on-rails ruby cookies bcrypt-ruby

我有一个登录版本的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标签的解决方案。我甚至将我的红宝石从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

1 个答案:

答案 0 :(得分:2)

拼写错误。正确的表格应为BCrypt

例如:

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

我希望这会有所帮助。