如何本地化表单对象上的错误消息

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

Rails 7 中是否可以本地化表单对象中的属性,包括

ActiveModel::Model

我有以下内容

class  ProductionDataForm
  include ActiveModel::Model

  attr_accessor :monitorable_type

  validates :monitorable_type,  presence: true
end

monitorable_type
丢失时,我想显示
Post can't be blank
而不是
Monitorable type can't be blank

因此,正如我对继承自

ApplicationRecord
的对象所做的那样,我尝试定义

en:
  activerecord:
    attributes:
      production_data_form:
        monitorable_type: "post"

但是不起作用。

ruby-on-rails internationalization
1个回答
0
投票
class ProductionDataForm
  include ActiveModel::Model
  include ActiveModel::Attributes
  attribute :monitorable_type, :string # ?
  validates :monitorable_type, presence: true
end
en:
  activemodel:
    attributes:
      production_data_form:
        monitorable_type: "post"
© www.soinside.com 2019 - 2024. All rights reserved.