更新个人资料而不更新密码 [英] Updating profile without updating password

查看:85
本文介绍了更新个人资料而不更新密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够更新我的用户信息,而不必让用户每次编辑任何其他属性时都设置密码.

I want to be able to update my user information without having to have the user set a password each time they edit any other attribute.

我当前的验证是:

  validates :password, presence: true, length: { minimum: 8 }
  validates :password_confirm, presence: true

我该如何设置此条件?仅当password和password_confirm属性位于参数中时,才需要进行这些验证是很明智的.我可以使用一些有关如何实现此目标的想法.谢谢.

how can I make this conditional? It would be clever to only require these validations if the password and password_confirm attribues were in the params. I could use some idea about how to achieve this. Thanks.

推荐答案

我认为应该这样做:

validates :password, presence: true, length: { minimum: 8 }
validates :password_confirm, presence: true, :if => Proc.new { |a| a.password_changed? || a.new_record? }

有关 ActiveModel :: Dirty 上的文档. >方法.每次更改属性时,在password上运行状态验证都应该没有问题,因为该属性将持久存在数据库中,因此它将始终通过(而password_confirm不会持久存在,因此需要有条件).

See the docs on ActiveModel::Dirty for more on the _changed? methods. There should be no problem running the presence validation on password every time you change an attribute since this will persist in the DB, so it will always pass (whereas password_confirm will not persist and thus requires a conditional).

这篇关于更新个人资料而不更新密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆