bcrypt LoadError:无法加载此类文件

时间:2014-10-24 18:04:03

标签: ruby-on-rails ruby ruby-on-rails-4 load bcrypt

我正在尝试为我的Rails应用设置登录功能,当我按下登录按钮时,我收到了一条bcrypt错误消息:

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

是否有其他人收到此错误?我有最新版本的bcrypt,我正在按照教程告诉我的那样做。

用户模型:我在据称错误的行周围放了星号。

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

会话控制器:

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'

会话/新视图:

<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>

4 个答案:

答案 0 :(得分:66)

如果您已经运行: bundle install 要安装bcrypt,只需重启rails服务器即可。它对我有用。

答案 1 :(得分:11)

确保您不仅运行bundle install,而且还要杀死服务器并重新加载它以确保它然后加载到新的gem中。你也可以检查一下你的宝石文件&#39; spring&#39;。如果那些也加载了,那么你想要对它进行评论,重新加载服务器然后再尝试。应该照顾所有可能性。

答案 2 :(得分:1)

杀死弹簧进程并重新启动Guard为我解决了这个问题:

$ ps aux | grep 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 -15 11526
$ kill -15 11529
$ kill ... 
$ kill ...

并重新启动:

$ bundle exec guard

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

答案 3 :(得分:0)

我遇到了同样的问题,但在我编辑Gemfile文件并取消注释该行之前无法解决它

    gem 'bcrypt', '~> 3.1.7' 

我最初安装了3.1.7版本,因为我担心是否可能与更高版本的兼容性问题,基于我在另一个解决此问题的解决方案中读到的内容,但3.1.7也失败了另一个错误消息。但是,3.1.11工作得很好,所以我在Gemfile中提出了注释

    gem 'bcrypt', '~> 3.1.11

再次运行bundle install。 这很有用。

相关问题