【问题标题】:Translation for nested_attributes in polymorphic relationship多态关系中nested_attributes的翻译
【发布时间】:2014-05-17 18:40:24
【问题描述】:

我正在尝试翻译多态模型的嵌套表单属性的标签。我正在使用 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: Файл

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

【问题讨论】:

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


    【解决方案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>
    

    【讨论】:

    • 我认为应该有办法通过正确设置语言环境文件来使其工作。
    • 谢谢,这行得通,但我想知道是否有办法通过 activerecord 模型属性使其工作?
    • @leemour 在文档中说明了如果您也使用 activerecord 该怎么做。那没用?
    • 您能指出相关的文档吗?正如您在我的问题中看到的那样,仅翻译模型的属性是行不通的。我看到了一些使用 model: model/attribute: translation 的解决方案,但也没有用。
    • 我有相同的设置。更改行的顺序后,它开始使用属性。奇怪)谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2014-10-11
    相关资源
    最近更新 更多