Rails针对嵌套属性的自定义错误消息

问题描述 投票:0回答:1

user.rb

has_many :friends
accepts_nested_attributes_for :friends

friend.rb

belongs_to :user
validates :name, presence: true

friends的属性以user形式提交。如果name字段为空,我会收到此错误:

Friends name can't be blank

我想将“朋友”重命名为其他内容,也许是“联系人”。与“name”相同 - 我想将其重命名为“Title”。我尝试了以下和许多变化但没有变化:

en.yml

en:
  activerecord:
    attributes:
      user/friend:
        attributes:
          name: "Title"
    errors:
      models:
        user/friend:
          attributes:
            name: "Title"
ruby-on-rails ruby-on-rails-5
1个回答
0
投票
en:
  activerecord:
    models:
      friend: Contact
    attributes:
      friend:
        name: Title

docs here

© www.soinside.com 2019 - 2024. All rights reserved.