如何验证 Rails 模型中的两个值不相等? [英] How do I validate that two values do not equal each other in a Rails model?
本文介绍了如何验证 Rails 模型中的两个值不相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用户模型,它有一个电子邮件和一个密码字段.为了安全起见,这些可能并不相等.我如何在我的模型中定义它?
I have a User model, which has an email and a password field. For security, these may not be equal to each other. How can I define this in my model?
推荐答案
validate :check_email_and_password
def check_email_and_password
errors.add(:password, "can't be the same as email") if email == password
end
但请记住,将密码存储为纯文本是个坏主意.您应该将其散列存储.尝试一些身份验证插件,如 authlogic 或 Restful 认证.
But keep in mind that storing password as a plain text is bad idea. You should store it hashed. Try some authentication plugin like authlogic or Restful authentication.
这篇关于如何验证 Rails 模型中的两个值不相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文