Rails i18n 属性无法通过 JSON API

Rails i18n Attributes Not Working via JSON API

我们有一个 RoR 4.2.5.1 API 服务器和一个 AngularJS 前端。

我正在尝试让国际主义发挥作用,并已将我的 en.yml 设置为

en:
  activerecord:
    attributes:
      nas:
        calledstationid: 'AP Mac'
    errors:
      models:
        nas:
          attributes:
            calledstationid:
              blank:
              invalid:
              taken: "has already been added to a location"

当我使用已占用的 calledstationid 创建 'nas' 时,我期望 "AP Mac has already been added to a location"。

相反,我得到了,"calledstationid..."

def create
  @nas = Nas.new(
    calledstationid: params[:box][:calledstationid]
  )
  respond_to do |format|
    if @nas.save
      format.json { render template: 'api/v1/boxes/show.json.jbuilder', status: 201 }
    else
      @errors = @nas.errors
      format.json { render template: 'api/v1/shared/index.json.jbuilder', status: 422 }
    end
  end
end

当我使用bang时,我在日志中看到calledstationid替换为apmac

所以...我的问题是,为什么@nas.errors 对象中的字段名称没有更新?我怎样才能让它适用于多个语言环境。

-- 编辑--

错误对象:

@messages={:calledstationid=>["has already been added to a location"]}>

你有 2 个选项来解决这个问题。

第一个:

编辑您的 yaml 文件并将消息替换为以下内容:

"%{attribute} has already been added to a location"

第二个:

您得到的是根据当前 YAML 配置的标准和正确的输出。但是要在不更改 YAML 的情况下获得您要查找的内容,需要使用 ActiveModel::Errors#full_messages。因为此方法 将属性名称添加到错误消息前