[RUBY] Resolve LoadError (cannot load such file --bcrypt) that occurs in rails test (Rails tutorial Chapter 6)

Keep track of any errors that occur as you progress through the rails tutorial. The hardware uses MacBook Air, and the development environment uses VScode.

Rails Tutorial Chapter 6 6.3 Adding a Secure Password

LoadError (cannot load such file --bcrypt) Synopsis until the error occurs

Generate a migration file called add_password_digest_to_users and perform the migration on the database.

$ rails generate migration add_password_digest_to_users password_digest:string
$ rails db:migrate

Write bcrypt in the Gemfile and run $ bundle install.

Gemfile


gem 'rails',          '5.1.6'
gem 'bcrypt',         '3.1.12'
$ bundle install

Next, add has_secure_password in the User model.

app/models/user.rb


class User < ApplicationRecord
  ~abridgement~
  has_secure_password
end

here

$ rails test

When you run, the validation added by has_secure_password should cause an error.

However, I got the following error:

$ rails test
LoadError: cannot load such file -- bcrypt
~abridgement~

As a test, when I run rails console and execute User.new, ...

$ rails console

Running via Spring preloader in process 18909
Loading development environment (Rails 5.1.7)

WARNING: This version of ruby is included in macOS for compatibility with legacy software. 
In future versions of macOS the ruby runtime will not be available by 
default, and may require you to install an additional package.

>User.new
You don't have bcrypt installed in your application. Please add it to your Gemfile and run bundle install
Traceback (most recent call last):
        3: from (irb):1
        2: from app/models/user.rb:1:in `<top (required)>'
        1: from app/models/user.rb:10:in `<class:User>'
LoadError (cannot load such file -- bcrypt)

The bcrypt that should have been installed is not loaded at all ... (crying)

[Rails] "cannot load such file --bcrypt_ext" error support when installing bcrypt [Windows]

If this solution seems to be the best, but it doesn't solve it at all ..., try the following solution.

Solution Update Ruby

First,

$ rbenv versions

Check the ruby version with. Then uninstall that ruby simasu

$rbenv uninstall Confirmed version

Then specify the new version and install it. (Here 2.6.5)

$ rbenv install 2.6.5

After the installation is complete, do the following:

$ rbenv global 2.6.5
$ rbenv rehash

This should change the version of ruby. (You can check with $ ruby -v)

Reference: How to use and how rbenv works

Finally, change / check the contents of the Gemfile.

source 'https://rubygems.org'

ruby '2.6.5'← Addendum
gem 'bcrypt'← You do not have to specify the version (bcrypt)-I don't need ruby or macOS)

▲ Here, specify the version of ruby and describe so that the latest version of bcrypt can be installed without specifying the version.

$ gem list bcrypt And if bcrypt is included, $ gem uninstall bcrypt) To delete it.

Do bundle install to install bcrypt.

$ bundle install

Then, when you run $ rails console, WARNING will be resolved and ** the error will be resolved. ** **


If it helps ** I would be grateful if you could click the LGTM button. ** ** Let's do our best to learn Rails together! : raised_hand_tone1:

Recommended Posts

Resolve LoadError (cannot load such file --bcrypt) that occurs in rails test (Rails tutorial Chapter 6)
[Error] `cannot load such file --mime/types/data (LoadError)`
When you get LoadError (cannot load such file --ruby_file) when you require in Ruby
[Ruby on Rails Tutorial] Error in the test in Chapter 3
How to resolve errors that occur in the "Ruby on Rails" integration test
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 6
[Rails Struggle/Rails Tutorial] What you learned in Rails Tutorial Chapter 3
I can't deploy! Resolve an error that can't be pushed to heroku (Rails Tutorial Chapter 1)
rails tutorial Chapter 6
Rails tutorial test
rails tutorial Chapter 1
rails tutorial Chapter 7
rails tutorial Chapter 5
rails tutorial Chapter 9
rails tutorial Chapter 8
Resolve Gem :: FilePermissionError when running gem install rails (Rails Tutorial Chapter 1)