我試圖讓has_secure_password和mongoid一起玩。我下面Railscasts#270,但是當我用一個用戶名/密碼簽到,我得到的錯誤:Rails 3.1 Mongoid has_secure_password
undefined method `find_by_email' for User:Class
我看到了類似的帖子(http://stackoverflow.com/questions/6920875/mongoid但是,按照它的建議,我仍然得到相同的錯誤。
這裏是我的模型:
class User
include Mongoid::Document
include ActiveModel::SecurePassword
validates_presence_of :password, :on => :create
attr_accessible :email, :password, :password_confirmation
field :email, :type => String
field :password_digest, :type => String
has_secure_password
end
控制器:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to root_url, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Logged out!"
end
end
感謝。
選項2偉大的工作。謝謝! – Hinchy