Rails i18n ActiveModel:翻译缺失验证的错误消息

Rails i18n ActiveModel: Translate error message for absence validation

提问人:Libby 提问时间:3/29/2022 更新时间:3/30/2022 访问量:599

问:

我在服务类中有此验证:

class Users::Updater
  include ActiveModel::Validations

  validates(:frozen_identification_data, absence: { message: 'Identification fields cannot be changed at this time' } )
end

我正在尝试将此错误消息移动到语言环境文件中:

en:
  activemodel:
    errors:
      users/updater:
        attributes:
          frozen_identification_data:
            absence: "Identification fields cannot be changed at this time"

但是当我重现测试用例时,错误消息是 .我假设这里是不正确的,但我在 Google 上找不到任何这种用法的例子。有谁知道如何翻译此验证?Frozen identification data must be blankabsence

Ruby-on-Rails 错误处理 本地 Rails-I18N

评论

1赞 Deepesh 3/29/2022
而不是尝试一次作为关键absencepresent
0赞 Libby 3/30/2022
@Deepesh奏效了。如果你给出这个答案,我会投赞成票并接受它。

答:

1赞 Deepesh 3/30/2022 #1

从导轨:https://guides.rubyonrails.org/i18n.html#error-message-interpolation

对于密钥应为:absencepresent

en:
  activemodel:
    errors:
      users/updater:
        attributes:
          frozen_identification_data:
            present: "Identification fields cannot be changed at this time"