【问题标题】:How to display index translation (rails-i18n) in Rails?如何在 Rails 中显示索引翻译(rails-i18n)?
【发布时间】:2020-02-18 07:12:23
【问题描述】:

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'

但是如何在不标明每个索引的情况下组织翻译呢?

【问题讨论】:

  • 嗨,我也有同样的问题。您找到任何解决方法了吗?

标签: ruby-on-rails activerecord indexing message rails-i18n


【解决方案1】:

尝试为模型设置翻译,而不将其嵌套在其关联中。类似于下面的 sn-p。

en:
  activerecord:
    models:
      inv_item: Inventory Item
    attributes:
      inv_item:
        serial_num: Serial Number

Rails Guide

【讨论】:

    猜你喜欢
    • 2017-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    • 2015-03-25
    • 2011-10-11
    • 1970-01-01
    相关资源
    最近更新 更多