Rails:has_many中子对象名称的自定义错误消息?

时间:2018-09-19 17:05:42

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

我有两个模型:

Category
  has_many :inventories

Inventory
  belongs_to :category

如果我正在创建类别并收到错误,则现在显示为:

Inventories unit of purchase can't be blank

我该怎么说呢?

Inventory unit of purchase can't be blank

我已经在en.yml中尝试了各种组合,并考虑了折线规则,但是似乎无法弄清楚这个窍门。

  

编辑

对于en.yml,我已经尝试过:

activerecord:
  models:
    inventory: "Inventory"

1 个答案:

答案 0 :(得分:0)

假设Inventory的属性名称为unit_of_purchase,应该这样做

en:  
  activerecord:    
    errors:
      models:
        inventory:
          attributes:
            unit_of_purchase:
              blank: "can't be blank"