2011-04-20 42 views
1

我想設置用戶模型成功地將用戶保存在數據庫中,但我受阻,NameError:未定義的局部變量或方法`hashed_pa​​ssword'爲#<用戶:0x000001029fef18 >未定義的局部變量或方法`hashed_pa​​ssword'爲用戶模型

用戶模式:


require 'digest' 
class User < ActiveRecord::Base 
    attr_accessor :password 

    validates :email, :uniqueness => true, 
        :length => { :within => 5..50 }, 
        :format => { :with => /^[^@][\w.-][email protected][\w.-]+[.][a-z]{2,4}$/i } 
    validates :password, :confirmation => true, 
         :length => { :within => 4..20 }, 
         :presence => true, 
         :if => :password_required? 

    has_one :profile 
    has_many :articles, :order => 'published_at DESC, title ASC', 
         :dependent => :nullify 
    has_many :replies, :through => :articles, :source => :comments 

    before_save :encrypt_new_password 

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

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

    protected 
    def encrypt_new_password 
     return if password.blank? 
     self.hashed_password = encrypt(password) 
    end 

    def password_required? 
     hashed_password.blank? || password.present? 
    end 

    def encrypt(string) 
     Digest::SHA1.hexdigest(string) 
    end 
end 
 

回答

0

我的第一個賭注是hashed_pa​​ssword沒有被定義爲模型中的一列。可能要檢查此特定型號的遷移文件

1

使用遷移將hashed_password字段添加到您的users表中。目前它缺失。

+0

謝謝添加該字段,它的工作 – John 2011-04-20 05:23:34

相關問題