简体   繁体   English

bcrypt LoadError:无法加载此类文件

[英]bcrypt LoadError: Cannot load such file

I'm trying to set up a login function for my Rails app, I'm getting a bcrypt error message when I press the login button:我正在尝试为我的 Rails 应用程序设置登录功能,当我按下登录按钮时,我收到一条 bcrypt 错误消息:

LoadError in SessionsController#create
cannot load such file -- bcrypt

Is anyone else getting this error?还有其他人收到此错误吗? I have the latest version of bcrypt and I'm following exactly what the tutorial told me to do.我有最新版本的 bcrypt,我完全按照教程告诉我的去做。

User Model: I put asterisks around the line where the error allegedly is.用户模型:我在错误所在的行周围加上星号。

class User < ActiveRecord::Base
  ****has_secure_password****
end

Sessions Controller:会话控制器:

class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.find_by(id: params[session][:id])
    if user && user.authenticate(params[:session][:password])
      log_in user
      redirect_to root_path
    else
      flash.now[:danger] = 'Invalid'
      render 'new'
    end
  end

  def destroy
  end
end

ApplicationController:应用控制器:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  include SessionsHelper
end

SessionsHelper:会话助手:

module SessionsHelper

  def log_in(user)
    session[:user_id] = user.id
  end
end

Gemfile:宝石档案:

gem 'bcrypt', '~> 3.1.7'

Sessions/new View:会话/新视图:

<div id= "admin-sign-in">
  <%= form_for(:session, url: login_path) do |f| %>

    <%= f.label :id %>
    <%= f.text_field :id %>

    <%= f.label :password %>
    <%= f.password_field :password %>

    <%= f.submit "Log in", class: "btn btn-primary" %>
  <% end %>
</div>

After running bundle install to install bcrypt , just restart the rails server .运行bundle install安装bcrypt只需重新启动 rails 服务器
That should help your application to load the bcrypt dependency.这应该有助于您的应用程序加载 bcrypt 依赖项。

make sure you not only run bundle install, but that you ALSO kill the server and reload it to make sure it then loads in the new gems.确保您不仅运行 bundle install,而且还杀死服务器并重新加载它以确保它随后加载到新的 gems 中。 you can also check your gemfile for 'spring'.您还可以检查您的 gemfile 中的“spring”。 if thats loaded too, you'll want to comment that out, reload the server and try it then.如果那也加载了,你会想把它注释掉,重新加载服务器然后尝试。 that should take care of all possibilities.这应该考虑到所有的可能性。

Killing spring process and restarting Guard resolved the issue for me:杀死 spring 进程并重新启动 Guard 为我解决了这个问题:

$ ps aux | grep spring

returned four spring processes:返回四个spring进程:

ubuntu     11526  0.0  0.0 298748 24348 pts/1    Sl   22:08   0:00 spring server | mh03_sample_app | started 16 mins ago
ubuntu     11529  0.4  0.1 531764 79204 ?        Ssl  22:08   0:04 spring app    | mh03_sample_app | started 16 mins ago | test mode 
...
...

kill (one by one):杀死(一一):

$ kill -15 11526
$ kill -15 11529
$ kill ... 
$ kill ...

and restart:并重新启动:

$ bundle exec guard

For a nice explanation see Michael Hartl's Rails Tutorialhttps://www.railstutorial.org/book/static_pages#aside-processes有关很好的解释,请参阅 Michael Hartl 的 Rails 教程https://www.railstutorial.org/book/static_pages#aside-processes

I had the same issue, but couldn't resolve it until I edited the Gemfile file, and uncommented the line我遇到了同样的问题,但直到我编辑了 Gemfile 文件并取消注释该行才能解决它

    gem 'bcrypt', '~> 3.1.7' 

I initially installed version 3.1.7 because I was worried if there were maybe compatibility issues with the later versions, based on something I read in another solution to this problem, but 3.1.7 also failed with another error message.我最初安装了 3.1.7 版,因为我担心后续版本是否存在兼容性问题,基于我在另一个解决方案中读到的内容,但 3.1.7 也因另一条错误消息而失败。 However, 3.1.11 worked perfectly, and so I bumped up the comment in the Gemfile to read然而,3.1.11 运行得很好,所以我在 Gemfile 中找到了注释来阅读

    gem 'bcrypt', '~> 3.1.11

and ran bundle install again.并再次运行 bundle install 。 This worked.这奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM