2011-09-30 4 views
6

mongoid와 함께 nice를 재생하기 위해 has_secure_password를 얻으려고합니다. 레일 3.1 몽고이 has_secure_password

undefined method `find_by_email' for User:Class 

내가 (비슷한 게시물을 참조 http://stackoverflow.com/questions/6920875/mongoid : 나는 # 270 Railscasts을 다음거야 내가 사용자 이름/암호로 로그인했습니다 때, 그러나, 나는 오류 - 및 - 보안 - 비밀 번호) 그러나, 나는 여전히 같은 오류가 발생 제안으로 일을했다.

여기 내 모델 :

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 

감사합니다.

답변

11

옵션 1 : 컨트롤러에서, 당신은

user = User.where(:email => params[:email]).first 

옵션 2를 사용한다 : 모델에서는

def self.find_by_email(email) 
    where(:email => email).first 
end 
+0

옵션 2 큰했다. 감사! – Hinchy

0

가 Mongoid가 find_by_attribute 방법을 지원하지 않습니다 정의해야합니다.

당신은 더 나은 대신 where을 사용해야합니다

User.where(:email => email).all