Rails中has_secure_password报错:cannot load such file -- bcrypt求助
bcrypt LoadError in Ruby on Rails Authentication Hey there, let's work through this bcrypt LoadError you're facing when setting up simple auth in Rails. I've run into this exact issue before, so here are the steps to get it sorted:
Make sure your Gemfile is set up correctly
Instead of installing bcrypt directly from the git repo, use the official gem entry in your Gemfile. Add this line (if it's not already there):gem 'bcrypt'Then run
bundle installin your terminal. Direct git installs can sometimes cause compatibility issues with your Rails version, so using the gem from RubyGems is the safer route.Uninstall any existing git-based bcrypt installations
If you installed bcrypt from the GitHub repo earlier, it might be conflicting with the official gem. Run this command to remove it:gem uninstall bcryptWhen prompted, select the version that came from the git repo (it'll have a git URL listed next to it). Then re-run
bundle installto get the correct version.Restart your Rails Console
The Rails Console caches your gem environment when it starts up. Even if you install bcrypt after opening the console, it won't pick up the new gem until you close the current console and open a new one withrails console.Check for Spring caching issues
Rails uses Spring to speed up development, but it can sometimes hold onto old gem data. Stop Spring with:spring stopThen open a new Rails Console and try
User.allagain.Verify system dependencies (if all else fails)
Bcrypt requires system-level compilation tools to install properly. If you're on macOS, make sure you have Xcode Command Line Tools installed (runxcode-select --install). On Linux, install build essentials (for Ubuntu/Debian:sudo apt-get install build-essential). After installing these, runbundle installagain.
Once you've gone through these steps, your has_secure_password should work without the LoadError, and User.all should run smoothly.
内容的提问来源于stack exchange,提问作者Rafael

