ruby-on-rails - 如何在 Rails 中显示索引翻译(rails-i18n)?
问题描述
Rails 5 使用(嵌套属性上的错误索引)为模型添加了错误索引:
class Order
has_many :operations, index_errors: true
accepts_nested_attributes_for :operations
end
class Operation
has_many :inv_items, index_errors: true
accepts_nested_attributes_for :inv_items
end
作为调用 order.errors.full_messages 的结果,我收到以下形式的错误:
{:"operations[0].inv_items[0].serial_num"=>["不能为空"], :"operations[0].inv_items[1].serial_num"=>["不能为空"]}
在文件 .yml 我可以直接设置翻译
en:
activerecord:
attributes:
warehouse/order/operations[0]/inv_items[0]:
serial_num: 'Serial number'
但是如何在不指明每个索引的情况下组织翻译呢?
解决方案
尝试为模型设置翻译而不将其嵌套在其关联中。类似于下面的片段。
en:
activerecord:
models:
inv_item: Inventory Item
attributes:
inv_item:
serial_num: Serial Number
推荐阅读
- python - 不完整付款意图的条纹自动取消
- html - grid-template-columns CSS 垂直显示而不是水平显示
- c++ - 使用 C++ 在 VS 2019 的开发人员命令提示符上编译错误
- visual-studio-code - 使用 Teams Toolkit 进行调试时,应用程序不会旁加载
- python - 有效地将 SciPy 优化方法应用于数组
- android - 被 Play Protect 屏蔽 [debug apk]
- x86 - 通用处理器和 HPC 处理器之间的主要区别是什么?
- flutter - Flutter LinearProgressIndicator 相对高度
- astyle - 如何使用 Astyle 用换行符终止文件?
- pandas - 根据另一列是否具有值来获得列值