论坛首页 编程语言技术论坛

accepts_nested_attributes_for: 关联表字段错误信息本地化

浏览 2004 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-01-14  

class Order
  has_many :order_informations
  accepts_nested_attributes_for :order_informations
end

class OrderInformation
  belongs_to :order
  validates_presence_of :quantity
end



使用 accepts_nested_attributes_for 后发现quantity的非空校验总是无法正确显示i18n后的效果。 后来用

  @order.errors.inpect


查看才发现错误格式是这个样子
#<OrderedHash {:operation_date=>[\"不能为空\"], :auditor=>[\"不能为空\"], :warehouse=>[\"不能为空\"], :\"order_informations.quantity\"=>[\"不是数字\", \"不能为空\"]}>"


所以要这样写本地化文件才可以正确的将关联表属性的错误信息本地化
zh-CN: 
  activerecord:
    models:
      order:
        order_informations:
          quantity: "数量"

论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics