覆盖 SecurePassword 的验证错误消息 [英] Override validation error message for SecurePassword

查看:40
本文介绍了覆盖 SecurePassword 的验证错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
完全自定义的 Rails 验证错误消息

我在玩 Rails 3.1rc1 并思考 SecurePassword 对我很有用.但是,我不喜欢默认的错误消息,密码摘要不能为空.如果我自己调用 validates_presence_of :password_digest 我可以通过 :message =>密码不能为空" 但因为它在框架中,我不确定如何覆盖消息以删除摘要"一词,这只会让 joe 用户感到困惑.有人知道怎么做吗?

I'm playing around with Rails 3.1rc1 and think SecurePassword will be useful for me. But, I don't like the default error message, Password digest can't be blank. If I called validates_presence_of :password_digest myself I could pass :message => "Password can't be blank" but because it's in the framework I'm not sure how to override the message to remove the word "digest" which will only confuse joe user. Anyone know how to do this?

尝试添加覆盖":validates_presence_of 像这样:

Tried adding an 'overriding': validates_presence_of like so:

class User < ActiveRecord::Base
  attr_accessible :email, :password

  has_secure_password
  validates_presence_of :password_digest, :message => "Password can't be blank"
end

但是当尝试提交一个空白密码时,您只会得到两倍的错误:

But when trying to submit a blank password you just get double the errors:

表单无效

  • 密码摘要不能为空
  • 密码摘要 密码不能是空白

推荐答案

我相信你可以使用 Rails InternationalizationAPI 来改变这一点.

I believe you can use the Rails Internationalization API to change this.

在您的 config/locales/en.yml 文件中,添加以下内容:

In your config/locales/en.yml file, add the following:

en:
  activerecord:
    attributes:
      user:
        password_digest: "Password"

如果您的模型类不是用户",您需要适当地更改该行.

If your model class is something other than 'user', you'll need to change that line appropriately.

无论如何,这对我有用.

Anyway, this works for me.

这篇关于覆盖 SecurePassword 的验证错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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