2011-10-13 14 views
8

晚上好。我有個問題。我正在使用has_secure_password 和這個原因我有一個錯誤undefined method password_digest ='for#`,無法使用has_secure_password,password_digest錯誤

但我沒有這個方法!請幫忙,不知道該怎麼辦。我讀了如何解決這個問題,但它並沒有幫我(

這是我的用戶模型。請如果你能幫助。

class User < ActiveRecord::Base 

    attr_accessible :email, :password, :password_confirmation 
    has_secure_password 

    validates_presence_of :password, :on => :create 

    before_create { generate_token(:auth_token) } 

    def send_password_reset 
    generate_token(:password_reset_token) 
    self.password_reset_sent_at = Time.zone.now 
    save! 
    UserMailer.password_reset(self).deliver 
    end 

    def generate_token(column) 
    begin 
     self[column] = SecureRandom.urlsafe_base64 
    end while User.exists?(column => self[column]) 
    end 
end 
+0

您正在使用哪個版本的Rails?我可能是錯的,但如果你使用最新的3.1.1,我認爲has_secure_password需要gem bcrypt-ruby〜> 3.0.0。 – christianblais

回答

20

你可能已經忘記了,以確保您的遷移支持用戶模型一列password_digest確保列存在並且它是一個字符串如果不存在,創建一個遷移來添加列

1

嘿,我也跟着RoR也出現了同樣的問題。這裏是你的bundle exec rake db:migrate失敗,因此password_digest列還沒有被添加到數據庫中。我的控制檯抱怨用戶數據庫已存在。我用「SQLite瀏覽器」刪除了db/development.sqlite3。運行bundle exec rake db:migrate後,每次測試通過

3

我有同樣的問題,我在下面http://www.railstutorial.org/book/modeling_users 和我app/Controllers/users_controllers.rb沒有創建屬性的方法,我也用的git分享的便攜筆記本電腦之間的工作代碼火車和較大的家庭,這創建了遷移文件,但沒有應用它,我的工作用戶控制器在下面。

class UsersController < ApplicationController 
    def new 
     attr_accessor :name, :email, :password 
     def initialize(attributes = {}) 
     @name = attributes[:name] 
     @email = attributes[:email] 
     @password = attributes[:password] 
    end 

    def formatted_email 
     "#{@name} <#{@email}>" 
    end 
end 
相關問題