rails 4中的自定义字符串活动记录错误消息

时间:2014-08-09 07:34:05

标签: ruby-on-rails activerecord ruby-on-rails-4 rails-activerecord

我在yml文件中定义了一条activerecords错误消息:

en:
  activerecord:
    errors:
      models:
        participant:
          attributes:
            base:
              file_type_invalid: This file type %{file_name} is not acceptable

因此错误消息应为This file type xyz.zip is not acceptable

并且在相应的模型中,我为这样的错误添加了如下错误:

errors.add(:base, :file_type_invalid) 我在这里如何将file_name传递给错误消息?这不起作用

errors.add(:base, :file_type_invalid, file_name: 'xyz.zip')

那么如何解决这个问题呢?请注意,file_name不是该列的属性或值。

1 个答案:

答案 0 :(得分:0)

您应该可以直接使用I18n:

errors.add(:base, I18n.translate('activerecord.errors.models.participant.attributes.base.file_type_invalid', file_name: 'some filename'))

这有点违背了自动查找的目的,但据我所知add方法不支持错误字符串的参数。