在多态关系中翻译nested_attributes

时间:2014-05-17 18:40:25

标签: ruby-on-rails ruby ruby-on-rails-4 rails-i18n

我正在尝试翻译多态模型的嵌套表单属性的标签。我正在使用Rails 4.
对象关系:

class Question < ActiveRecord::Base
  has_many :attachments, as: :attachable,  dependent: :destroy
  accepts_nested_attributes_for :attachments
end

class Attachment < ActiveRecord::Base
  belongs_to :attachable, polymorphic: true
end

我的表格:

= form_for @question do |f|
  .form-group
    =f.text_area :body

  = fields_for :attachments do |a|
    .form-group
      = a.label :file
      = a.file_field :file

  = f.submit

我的ru.yml:

activerecord:
  attributes:
    question:
      attachment:
        file: Файл
      attachments:
        file: Файл
    attachment:
      file: Файл

不起作用。什么应该是语言环境结构?

1 个答案:

答案 0 :(得分:1)

根据:http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-label

你应该尝试:

helpers:
  label:
    yourmodelname:
      yourmodelattr: "Write your entire text here"

这是猜测您的I18n.locale设置为:ru

希望这会对你有所帮助。

引用文档:

本地化也可以完全基于属性名称的翻译(如果您使用的是ActiveRecord):

activerecord:
  attributes:
    post:
      cost: "Total cost"

输出:

f.label(:post, :cost)
# => <label for="post_cost">Total cost</label>