I’m trying to make a sample login with has_secure_password. The gem file is added, but this error persists:
undefined local variable or method `password_digest' for #<User:0x23129a0>
app/controllers/sessions_controller.rb:8:in `create'
In this file
sessions_controller.rb
line 8 looks like this:
if user and user.authenticate(params[:password])
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_name(params[:name])
if user and user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to admin_url , notice: "Login efetuado com sucesso."
else
flash[:notice] = 'Usuario ou senha errados'
redirect_to login_url
end
end
def destroy
session[:user_id] = nil
redirect_to login_url, notice: "Logout Efetuado com sucesso."
end
end
I agree with Béla that this is a duplicate. However, here are step-by-step instructions in case the original answer wasn’t clear enough.
To add the
password_digestto your user model…In the console do:
This will create a file something like:
/db/migrate/20120419234606_add_password_digest_to_users(The fourteen digits at the beginning of your file will be different than mine since it’s basically a time stamp for when you ran the generate command. You can read more on generating stand alone migrations here.)Open the migration file you just created and edit so it looks something like this:
Save and close the file. Then, in the console, run:
At this point, your user model should have the
password_digestattribute and (hopefully) you won’t get the error any more.