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>
after running bundle install
to install bcrypt
, do not forget to restart the rails server.
It worked for me.