新手问题 在 Model 里验证一个字段的时候,定制消息可以把消息里的字段名定制了吗?

wdrsam · 2015年11月03日 · 最后由 huacnlee 回复于 2015年11月03日 · 2363 次阅读

比如 name 字段,用:message 后,前面还是会输出字段的名字。怎么把它去掉或者改成其他的?

http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models

'zh-CN':
  activerecord:
    models:
      user: "账号"
    attributes:
      user:
        login: "用户名"
        password: "密码"

#1 楼 @huacnlee 麻烦大哥!嘿嘿 再问一个消息定制的问题,我用了 has_secure_password。那封装在里面的验证,例如:不能为空,以及密码和密码确认 2 个字段必须相同,等等的验证的消息改在哪里修改?

#1 楼 @huacnlee 查了文档,有 3 个验证。

  • Password must be present on creation

  • Password length should be less than or equal to 72 characters

  • Confirmation of password (using a password_confirmation attribute)

不知道该怎么在

has_secure_password(options = {})

options 里分别 3 个验证时的定制消息..

# config/locales/en.yml
en:
  activerecord:
    attributes:
      user:
        email: "E-mail address"
    errors:
      models:
        user:
          attributes:
            email:
              blank: "is required"

#4 楼 @wdrsam 用 rails-i18n 这个 Gem

需要 登录 后方可回复, 如果你还没有账号请 注册新账号