2011-09-30 23 views
6

मैं mongoid के साथ अच्छा खेलने के लिए has_secure_password प्राप्त करने का प्रयास कर रहा हूं।रेल 3.1 Mongoid has_secure_password

undefined method `find_by_email' for User:Class 

मैं एक ऐसी ही पोस्ट देखें (http://stackoverflow.com/questions/6920875/mongoid: मैं Railscasts अनुसरण कर रहा हूँ # 270 है, लेकिन जब मैं एक उपयोगकर्ता नाम/पासवर्ड के साथ प्रवेश द्वारा के लिए, मैं त्रुटि मिलती है -और-सुरक्षित-पासवर्ड) हालांकि, जैसा कि सुझाव दिया गया है, मैंने अभी भी वही त्रुटि प्राप्त की है।

यहाँ मेरी मॉडल है:

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 
संबंधित मुद्दे