Ruby-on-rails bcrypt LoadError:无法加载此类文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26553696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:58:12  来源:igfitidea点击:

bcrypt LoadError: Cannot load such file

ruby-on-railsrubyruby-on-rails-4loadbcrypt

提问by Joe Morano

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>

回答by Bruno Paulino

after running bundle installto install bcrypt, do not forget to restart the rails server.
It worked for me.

运行bundle install安装后bcrypt,不要忘记重启rails服务器
它对我有用。

回答by Shaun Sweet

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. you can also check your gemfile for '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.

确保您不仅运行 bundle install,而且还杀死服务器并重新加载它以确保它随后加载到新的 gems 中。您还可以检查您的 gemfile 中的“spring”。如果那也加载了,你会想把它注释掉,重新加载服务器然后尝试。这应该考虑到所有的可能性。

回答by Graeme Campbell

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. However, 3.1.11 worked perfectly, and so I bumped up the comment in the Gemfile to read

我最初安装了 3.1.7 版,因为我担心后续版本是否存在兼容性问题,基于我在另一个解决方案中读到的内容,但 3.1.7 也因另一条错误消息而失败。然而,3.1.11 完美运行,所以我在 Gemfile 中添加了注释来阅读

    gem 'bcrypt', '~> 3.1.11

and ran bundle install again. This worked.

并再次运行 bundle install 。这奏效了。

回答by Arta

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 Tutorial https://www.railstutorial.org/book/static_pages#aside-processes

有关很好的解释,请参阅 Michael Hartl 的 Rails 教程https://www.railstutorial.org/book/static_pages#aside-processes