Remson
Remson

Reputation: 73

rails 4.2.4 Uninitialized Constant User::BCRYPT

I have rails app with a login auth that i got from RoR tutorial by Micheal Hartl. The login has a remember me for cookie logins. The problem is when a user cookie exist and i start my rails app, i got this error

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'

Logging in with no remember-me works fine. I have tried solutions that I found like putting require tag on gemfile or in user model. I even dowgraded my ruby from 2.2.3 to 2.1.7 to see if it would fix but it doesnt.

this is my user Model

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

end

Upvotes: 2

Views: 1992

Answers (1)

huzefa biyawarwala
huzefa biyawarwala

Reputation: 657

There is an error in your spelling . The correct form should be BCrypt.

For example :

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

I hope this helps .

Upvotes: 2

Related Questions