2011-09-30 21 views
6

mongoidでniceを再生するためにhas_secure_passwordを取得しようとしています。 Rails 3.1 Mongoid 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:あなたのモデルでは、あなたが

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

オプション2がとてもうまくいきました。ありがとう! – Hinchy

0

Mongoidはfind_by_attributeメソッドをサポートしていない定義する必要があります:あなたのコントローラでは、あなたが

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

オプション2を使用する必要があります。

方が良い代わりにwhereを使用する必要があります。

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