Rails i18n属性不能通过JSON API工作

时间:2016-01-31 11:59:44

标签: ruby-on-rails internationalization rails-i18n

我们有一个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"

当我创造' nas'有了一个被称为callstationid,我期待" AP Mac已经被添加到一个位置"。

相反,我得到了,#34; callstationid ..."

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

当我使用爆炸时,我可以看到日志中的ap mac替换了被称为

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

- 编辑 -

错误对象:

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

1 个答案:

答案 0 :(得分:1)

您有2个选项可以解决此问题。

<强>首先

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

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

<强>第二

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