I18n中的字符串插值用于有效记录验证错误

时间:2018-08-04 13:21:26

标签: rails-i18n

我有一个我不想覆盖的非用户友好的验证错误消息:

Cannot delete record because dependent [unfriendly model names] exist

关联所在<​​/ p>

has_many :unfriendly_model_names, dependent: :restrict_with_error

我希望它成为

Cannot delete record because dependent [nice readable model names] exist

我希望可以使用I18n,但是如何以及在什么时候将友好的字符串传递到record插值中?

en-GB:
  activerecord:
    errors:
      messages:
        restrict_dependent_destroy:
          has_many: "Cannot delete record because dependent %{record} exist"

1 个答案:

答案 0 :(得分:0)

您必须本地化关联属性。因此,如果您要破坏受错误限制的许多user的{​​{1}}:

posts

原因在这里:https://github.com/rails/rails/blob/6-0-stable/activerecord/lib/active_record/associations/has_many_association.rb#L20