0
我有以下的模型,我試圖用這個Rails Validates Prevents Save確定password_required的正確位置?用before_save密碼加密
class User < ActiveRecord::Base
before_save :encrypt_password
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 4..12 },
:if => :password_required?
def password_required?
self.new_record? or self.password?
end
#
# where we encrypt on creation
#
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
我得到一個錯誤關閉密碼驗證的用戶模式:
undefined method `password?' for #<User:0x007fc8e0473be0>
我應該檢查self.password_hash?還有另一種更新密碼的形式。在這種情況下關閉驗證的最佳策略是什麼?
THX
thx,這是有道理的,但仍然收到錯誤「未定義的方法'密碼?爲#<用戶:0x007fc8e05da5b0>'。 Noob在鐵軌上。 self.password的語法是什麼?離 – timpone 2012-03-04 13:18:35