Ruby on Rails で深くネストされた属性の検証エラー メッセージをローカライズするにはどうすればよいですか?
概要
私の Ruby on Rails 7 アプリには、多数の CargoItem を含む Booking があり、その Booking には多数の CargoItemDocuments (基本的にアップロード添付ファイルが含まれるだけです) を含めることができます。
class Booking < ApplicationRecord
has_many :cargo_items
accepts_nested_attributes_for :cargo_items
end
class CargoItem < ApplicationRecord
belongs_to :booking
has_many :cargo_item_documents
accepts_nested_attributes_for :cargo_item_documents
end
class CargoItemDocument < ApplicationRecord
belongs_to :cargo_item
has_one_attached :upload
end
残念ながら、深くネストされた CargoItemDocuments のローカライズされた検証メッセージは正しく表示されません。これは私のドイツ語ローカリゼーション ファイルです。
de:
activerecord:
attributes:
booking:
number: "Nummer"
date: "Datum"
cargo_item:
title: "Titel"
weight: "Gewicht"
cargo_item_documents:
upload: "Datei" # <-- Not working!
したがって、無効なファイルタイプをアップロードすると、ドイツ語の検証エラーメッセージの代わりに、現在次のメッセージが表示されます。
これはどうすれば修正できますか?
解決策
OK、3週間後、これが解決策であることがわかりました。
de:
activerecord:
attributes:
booking:
number: "Nummer"
date: "Datum"
cargo_item:
title: "Titel"
weight: "Gewicht"
cargo_items/cargo_item_documents:
upload: "Datei"