我从Rails教程中找到的用户模型中推导出发现here以了解有关创建模型的更多信息。我试图给用户一个确认标志,该标志最初设置为false,直到用户通过点击注册后发送的自动邮件中的链接确认其身份。将Rails模型属性结果添加到RecordNotSaved错误中
在我添加确认的属性之前,所有工作都正常。我通过迁移向数据库添加了确认列,因此在我看来,错误发生在before_save :confirmed_false
逻辑中的某处。
有人可以帮助我吗?用户模型如下。
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
validates :password, :presence => true,
:confirmation => true,
:length => { :within => 6..40 }
before_save :encrypt_password
before_save :confirmed_false
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
def self.authenticate(email, submitted_password)
user = find_by_email(email)
return nil if user.nil?
return user if user.has_password?(submitted_password)
end
private
def confirmed_false
self.confirmed = false if new_record?
end
def encrypt_password
self.salt = make_salt if new_record?
self.encrypted_password = encrypt(password)
end
def encrypt(string)
secure_hash("#{salt}--#{string}")
end
def make_salt
secure_hash("#{Time.now.utc}--#{password}")
end
def secure_hash(string)
Digest::SHA2.hexdigest(string)
end
1,1 Top
你得到了什么确切的错误? – rubyprince 2011-03-22 06:48:23