使用前缀名称转换模型的rails嵌套属性

时间:2018-05-16 19:49:19

标签: ruby-on-rails activerecord prefix rails-i18n

我的项目有两个带有前缀名称的模型:
专业人员::公司专业人员::服务

专业版::公司模型

class Professional::Company < ApplicationRecord
  has_many :professional_services, class_name: 'Professional::Service',
                                       dependent: :destroy,
                                       foreign_key: 'professional_company_id',
                                       inverse_of: :professional_company  
  accepts_nested_attributes_for :professional_services  
end

专业版::服务型号

class Professional::Service < ApplicationRecord
  belongs_to :professional_company, class_name: 'Professional::Company',
                                    foreign_key: 'professional_company_id',
                                    inverse_of: :professional_services
end

pt-Br.yml

pt-BR:
  activerecord:
    attributes:
      professional/company:
        name: 'Name'
        description: 'Description'
      professional/service:
        description: 'Description service'
        name: 'Title service'

我使用专业版::服务的嵌套属性保存专业版::公司
在错误的情况下我如何翻译  Professional :: Service嵌套属性?

1 个答案:

答案 0 :(得分:0)

我用下面的代码解决了这个问题

pt-BR:
  activerecord:
    attributes:
      professional/company:
        name: 'Name'
        description: 'Description'
      professional/service:
        description: 'Description service'
        name: 'Title service'
      professional/company/professional_services:
        description: 'Description service'
        name: 'Title service'

基本上你需要把这段代码放在属性级别:

 prefix/model_name/method_of_association: 
    nested_attribute: 'The translate'