簡體   English   中英

bcrypt LoadError:無法加載此類文件

[英]bcrypt LoadError: Cannot load such file

我正在嘗試為我的 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

應用控制器:

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

會話助手:

module SessionsHelper

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

寶石檔案:

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>

運行bundle install安裝bcrypt只需重新啟動 rails 服務器
這應該有助於您的應用程序加載 bcrypt 依賴項。

確保您不僅運行 bundle install,而且還殺死服務器並重新加載它以確保它隨后加載到新的 gems 中。 您還可以檢查您的 gemfile 中的“spring”。 如果那也加載了,你會想把它注釋掉,重新加載服務器然后嘗試。 這應該考慮到所有的可能性。

殺死 spring 進程並重新啟動 Guard 為我解決了這個問題:

$ ps aux | grep spring

返回四個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

我遇到了同樣的問題,但直到我編輯了 Gemfile 文件並取消注釋該行才能解決它

    gem 'bcrypt', '~> 3.1.7' 

我最初安裝了 3.1.7 版,因為我擔心后續版本是否存在兼容性問題,基於我在另一個解決方案中讀到的內容,但 3.1.7 也因另一條錯誤消息而失敗。 然而,3.1.11 運行得很好,所以我在 Gemfile 中找到了注釋來閱讀

    gem 'bcrypt', '~> 3.1.11

並再次運行 bundle install 。 這奏效了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM