新手问题 问一个问题 关于 validates 和 I18n 的 (已解决)

themorecolor · July 05, 2013 · Last by yune replied at June 28, 2015 · 3027 hits
models/user.rb
validates :username, presence: true

然后配置错误信息

zh:
  activerecord:
    errors:
      models:
        user:
          attributes:
            username:
              blank: "用户名不能为空!"

但是在显示错误信息的时候显示

Username 用户名不能为空!

如何能把 Username 去掉,或者把错误信息里的 Username 也做成国际化

zh-CN:
  activerecord:
    models:
      category: 分类
    attributes:
      category:
        name: 分类名
        created_at: 创建时间
        updated_at: 更新时间
        products_count: 商品数
        properties_count: 属性数

类似这样不就可以了

#1 楼 @tumayun

activerecord:
  models:
    user: 用户
  attributes:
    user:
      username: 用户名

activerecord:
  errors:
    models:
      user:
        attributes:
          username: 
            blank: "用户名不能为空!"

这样嘛?但是 没有效果 啊

你在一个文件里?

你就不能分文件 或者合并下吗? activerecord被下面的覆盖了都...

#6 楼 @tumayun 分文件的话 文件名称没有关系 只要 开头的 zh:一样就可以了吧?

#6 楼 @tumayun

activerecord:
  models:
    user: 用户
  attributes:
    user:
      username: 用户名
  errors:
    models:
      user:
        attributes:
          username:
            blank: "用户名不能为空!"

OK 了 THX

如果有很多项验证怎么办?要写很多遍“不能为空” ?有没有简介的写法?

activerecord:
  models:
    user: "用户"
  attributes:
    user:
      name: "姓名"
      tel: "电话"
      job: "期望职位"
  errors:
    models:
      user:
        attributes:
          name:
            blank: "不能为空"
          tel:
            blank: "不能为空"
          job:
            blank: "不能为空"
You need to Sign in before reply, if you don't have an account, please Sign up first.