在示例登錄程序中,我有兩個類。會話和用戶。用戶表有兩個字段名稱,密碼。調用用戶類的身份驗證方法時,會話方法繼續獲得「NoMethodError」。用戶類認證NoMethodError
用戶類看起來如下:
class UserController < ApplicationController
def index
@user = User.find(:first, :order => 'RANDOM()')
end
def show
@user = User.find(:all)
end
def new
@user = User.new
#redirect_to user_path
end
def create
@user = User.new(params[:user])
@user.save
#redirect_to 'http://localhost:3000/user/show'
end
def self.authenticate(password)
#user = User.find_by_name(name)
if user.find_by_password(password)#match_password(password)
return true
else
return false
end
end
def self.match_password(password="")
encrypted_password == BCrypt::Engine.hash_secret(password, salt)
end
end
的會話類有創建方法,調用用戶的身份驗證FN。 這裏是創建methohd
def create
user = User.find_by_name(params[:session][:name].downcase)
if user && user.authenticate(params[:session][:password])
# Sign the user in and redirect to the user's show page.
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination' # Not quite right!
render 'new'
end
end
這裏是堆棧提前
Started POST "/sessions" for 127.0.0.1 at Sun Sep 30 15:21:02 -0400 2012
Processing by SessionsController#create as HTML
Parameters: {"commit"=>"Sign in", "session"=>{"name"=>"ar", "password"=>"[FILTERED]"}, "authenticity_token"=>"vxYHw/4JV2fJuvvBN4GVLv31aBl8ETEtQPBB/483q/Q=", "utf8"=>"✓"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."name" = 'ar' LIMIT 1
Completed 500 Internal Server Error in 4ms
NoMethodError (undefined method `authenticate' for #<User:0xb69650e4>):
app/controllers/sessions_controller.rb:10:in `create'
感謝, A.
您似乎已將'UserController'的控制器代碼與'User'的模型代碼混淆在一起。 –
對,將self.authenticate和self.match_password放入User