Rails 嵌套属性的更漂亮的错误消息?

Prettier error messages for Rails nested attributes?

以我的订单结账流程为例,

客人的订单

来宾属于用户

用户有很多phone个号码

number 是 phone number 的一个属性。

我会收到这样的错误消息:

[:"guest.user.phone_numbers.number", "is an invalid number"]

如何返回更漂亮的消息? 像 'Invalid phone number format.' 或 'Phone number is invalid'

我知道我可以从字符串中破解出来,但我希望有一种 Rails 定义关联名称的方法?有 I18n 支持?

In the event you need to access nested attributes within a given model, you should nest these under model/attribute at the model level of your translation file:

en:   
  activerecord:
    attributes:
     user/gender:
       female: "Female"
       male: "Male"

Then User.human_attribute_name("gender.female") will return "Female".
- Rails Guides - Translations for Active Record Models

en:
  activerecord:
    attributes:
      guest/user/phone_numbers:
        number: 'Phone number'