i'm having nomethoderror specifically:
nomethoderror (undefined method `verify_password' nil:nilclass):
when running ruby on rails server above error
and when attempting use method:
nomethoderror: undefined method `password_hash' #
class user < activerecord::base validates_presence_of :username, :salt, :password_hash def self.create_with_password(username, password) salt = securerandom.hex password_hash = self.generate_hash(password, salt) self.create( username: username, salt: salt, password_hash: password_hash ) end def verify_password(password) self.password_hash == user.generate_hash(password, self.salt) end def self.generate_hash(password, salt) digest = openssl::digest::sha256.new digest.update(password) digest.update(salt) digest.to_s end
thats code i'm using atm
i have managed solve ">nomethoderror: undefined method `password_hash' # class user < activerecord::base validates_presence_of :username, :salt, :password_hash" problem
although ">nomethoderror (undefined method `verify_password' nil:nilclass):" still persists, code
class applicationcontroller < actioncontroller::base # prevent csrf attacks raising exception. # apis, may want use :null_session instead. # protect_from_forgery with: :exception def home end def register render layout: 'sign' end # post request def login @username = params[:username] @password = params[:password] user = user.where(username: @username).first if valid = user.verify_password(@password) else if valid session[:signed_in] = true session[:username] = params[:username] redirect_to '/home' else redirect_to '/sign-in.html' end redirect_to '/sign-in.html' end end def logout reset_session redirect_to '/home' end end
i'm not 100% sure why problem persist if models working proof testing in console works
2.1.5 :001 > user.create_with_password('help', 'pls') (0.4ms) begin transaction sql (0.8ms) insert "users" ("password_hash", "username", "salt") values (?, ?, ?) [["password_hash", "f9216ad1e04d111473435268e0afac3cfa66eb53eb95bd42a793c7515790b707"], ["username", "help"], ["salt", "8697d61a06cf6b501823d991db978000"]] (11.6ms) commit transaction => #<user id: 7, username: "help", salt: "8697d61a06cf6b501823d991db978000", password_hash: "f9216ad1e04d111473435268e0afac3cfa66eb53eb95bd42a7..."> 2.1.5 :002 > user = _ => #<user id: 7, username: "help", salt: "8697d61a06cf6b501823d991db978000", password_hash: "f9216ad1e04d111473435268e0afac3cfa66eb53eb95bd42a7..."> 2.1.5 :003 > user => #<user id: 7, username: "help", salt: "8697d61a06cf6b501823d991db978000", password_hash: "f9216ad1e04d111473435268e0afac3cfa66eb53eb95bd42a7..."> 2.1.5 :004 > user.verify_password('pls') => true 2.1.5 :005 >
http://ipt-dynaman.c9.io/sign-in.html site im developing has problem
Comments
Post a Comment