【发布时间】:2011-08-25 13:18:13
【问题描述】:
在我的控制器中,我尝试更新用户实例的rank 属性(整数)。例如从 1 到 2。
我这样做是:
@user = User.find(params[:id])
@user.rank = 2
@user.save(:validate => false)
由于某种原因,正在保存的用户的密码被删除,这样他们就可以在没有密码的情况下登录我的网站。我尝试过使用和不使用:validate => false 参数。
有什么理由吗?帮助?非常感谢
型号代码
类用户
validates :login, :presence => true, :length => { :maximum => 15, :minimum => 4 }, :uniqueness => true
validates :fname, :presence => true, :length => {:minimum => 2 }
validates :lname, :presence => true, :length => {:minimum => 2 }
validates :email, :presence => true, :format => { :with => email_filter}, :uniqueness => { :case_sensitive => false }
validates :password, :presence => true, :confirmation => true, :length => { :within =>4..40 }
validates :lane_id, :presence => true
before_save :encrypt_password
has_many :reports
has_many :accomplishments
belongs_to :lane
def has_password?(submitted_password)
encrypted_password == encrypt(submitted_password)
end
def self.authenticate(login, submitted_password)
user = find_by_login(login)
return nil if user.nil?
return user if user.has_password?(submitted_password)
end
def self.authenticate_with_salt(id, cookie_salt)
user = find_by_id(id)
(user && user.salt == cookie_salt) ? user : nil
end
def current_report
report = (Report.order("created_at DESC")).find_by_user_id(@user.id)
end
private
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
结束
【问题讨论】:
-
你有前置过滤器还是后置过滤器?
-
不,只是默认的标准导轨形式
-
嗯,我有一个 before_save :encrypt_password。会这样做吗?我打电话 :validate => false 虽然...
-
发布您的用户模型的代码
-
好吧,我猜是因为我正在保存用户,并且有一个 before_save 过滤器来加密“不存在”的密码,所以它会变成空白?
标签: ruby-on-rails ruby ruby-on-rails-3