首页 > 解决方案 > NameError(未初始化的常量 BCrypt)

问题描述

当我尝试创建密码时,我在 Rails 控制台中不断收到此错误。

BCrypt::Password.create("foobar")
NameError (uninitialized constant BCrypt)

我的宝石文件:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.1'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.3', '>= 6.0.3.2'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
gem 'bcrypt', '~> 3.1.7' 

# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
gem 'rack-cors'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  gem 'listen', '~> 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem "active_model_serializers"
gem "jwt"

我的用户模型:

class User < ApplicationRecord
    has_secure_password
    belongs_to :school
    has_many :user_courses
    has_many :courses, through: :user_courses  
    has_many :user_resources
    has_many :resources, through: :user_resources
  end

我的用户架构:

  create_table "users", force: :cascade do |t|
    t.string "name"
    t.string "category"
    t.integer "school_id"
    t.boolean "permission", default: true
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

任何想法为什么会发生此错误?我也尝试在我的用户模型中要求 bcrypt 但仍然没有运气。我也尝试了 Password.new 但仍然没有。不知道此时还能做什么,任何帮助将不胜感激!

标签: ruby-on-railsauthenticationbcryptrails-console

解决方案


推荐阅读