Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.

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>
like image 594
Joe Morano Avatar asked Oct 24 '14 18:10

Joe Morano


4 Answers

After running bundle install to install bcrypt, just restart the rails server.
That should help your application to reload and pick up the newly installed bcrypt dependency.

like image 118
Bruno Paulino Avatar answered Nov 14 '22 09:11

Bruno Paulino


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.

like image 11
Shaun Sweet Avatar answered Nov 14 '22 09:11

Shaun Sweet


I had the same issue, but couldn't resolve it until I edited the Gemfile file, and uncommented the line

    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

    gem 'bcrypt', '~> 3.1.11

and ran bundle install again. This worked.

like image 4
Graeme Campbell Avatar answered Nov 14 '22 08:11

Graeme Campbell


Killing spring process and restarting Guard resolved the issue for me:

$ ps aux | grep spring

returned four spring processes:

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

like image 4
Arta Avatar answered Nov 14 '22 10:11

Arta