2013-09-24 25 views
0

我讀了一本書(開始Rails 3中),而是試圖on Rails的4未定義的身份驗證?在Rails的方法4

所以,一切,我有一個問題:

def self.authenticate(email, password) 
    user = where('email = ?', email) 
    return user if user && user.authenticated?(password) 
    end 

    def authenticated?(password) 
    self.hashed_password == encrypt(password) 
    end 

當我這樣做,在rails console

User.authenticate('[email protected]', 123) 

Rails給我回復了一個錯誤,Undefined method authenticated?

irb(main):034:0> User.authenticate('[email protected]', 123) 
    NoMethodError: ←[1m←[35mUser Load (1.0ms)←[0m SELECT "users".* FROM "users" WHERE (email = '[email protected]') 
    undefined method `authenticated?' for #<ActiveRecord::Relation []> 
      from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/relation/delegati 
    on.rb:121:in `method_missing' 
      from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-4.0.0/lib/active_record/relation/delegati 
    on.rb:68:in `method_missing' 
      from C:/Sites/rails-estudo-blog/app/models/user.rb:23:in `authenticate' 
      from (irb):34 
      from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `st 
    art' 
      from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `sta 
    rt' 
      from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (requ 
    ired)>' 
      from bin/rails:4:in `require' 
      from bin/rails:4:in `<main>' 
    irb(main):035:0> 

怎麼了?


  • 的Rails 4.0
  • 紅寶石2.0
  • Windows 7的64位

回答

1

,而不是user = where('email = ?', email)你應該有user = where('email = ?', email).first

+0

親愛的主啊,我不能相信。謝謝先生。 –

+0

不客氣。對我發生了很多次。 –