Rails 3 has_many 关系中nested_attributes 的 I18n 标签翻译

发布于 2024-10-08 10:32:17 字数 706 浏览 4 评论 0原文

使用:Rails 3.0.3,Ruby 1.9.2

关系如下:

class Person < ActiveRecord::Base
  has_many :contact_methods
  accepts_nested_attributes_for :contact_methods
end

class ContactMethod < ActiveRecord::Base
  attr_accessible :info
  belongs_to :person
end

现在,当我尝试在 I18n 中自定义 contact_method 标签时,它无法识别它。

en:
  helpers:
    label:
      person[contact_methods_attributes]: 
        info: 'Custom label here'

我也尝试过:

person[contact_method_attributes]

这对于 1-1 关系来说效果很好,例如

person[wife_attributes]: 
  name: 'My wife'

,但不适用于 person[wives_attributes]

提前致谢

Using: Rails 3.0.3, Ruby 1.9.2

Here's the relationship:

class Person < ActiveRecord::Base
  has_many :contact_methods
  accepts_nested_attributes_for :contact_methods
end

class ContactMethod < ActiveRecord::Base
  attr_accessible :info
  belongs_to :person
end

Now when I try to customize the contact_method labels in I18n, it doesn't recognize it.

en:
  helpers:
    label:
      person[contact_methods_attributes]: 
        info: 'Custom label here'

I have also tried:

person[contact_method_attributes]

This works just fine for 1-1 relationships, e.g.

person[wife_attributes]: 
  name: 'My wife'

but not person[wives_attributes]

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乱了心跳 2024-10-15 10:32:17

我这样做是这样的:

en:
  helpers:
    label:
      person[contact_methods_attributes][0]: 
        info: 'First custom label here'
      person[contact_methods_attributes][1]: 
        info: 'Second custom label here'

这很好,但当你有无限的选择时并不理想。我只是在表单生成器中指定一个自定义翻译键:)

en:
  helpers:
    label:
      person[contact_methods_attributes][any]: 
        info: 'Custom label here'

<% fields_for :contact_methods do |builder| %>
  <%= builder.label :info, t("helpers.person[contact_methods_attributes][any].info") %>
  <%= builder.text_field :info %>
<% end %>

编辑:
不知道这是否是一个新功能,但似乎这样做就像一个魅力:

en:
  helpers:
    label:
      person:
        contact_methods: 
          info: 'Custom label here'

I did this with :

en:
  helpers:
    label:
      person[contact_methods_attributes][0]: 
        info: 'First custom label here'
      person[contact_methods_attributes][1]: 
        info: 'Second custom label here'

Which is nice but not ideal when you have unlimited options.. I would just specify a custom translation key in the form builder :)

en:
  helpers:
    label:
      person[contact_methods_attributes][any]: 
        info: 'Custom label here'

<% fields_for :contact_methods do |builder| %>
  <%= builder.label :info, t("helpers.person[contact_methods_attributes][any].info") %>
  <%= builder.text_field :info %>
<% end %>

EDIT :
Don't know if it's a new feature but seems to work like a charm doing this :

en:
  helpers:
    label:
      person:
        contact_methods: 
          info: 'Custom label here'
帅冕 2024-10-15 10:32:17

在我的 Rails 3.2.13 应用程序中,属性标签是从嵌入属性的模型中自动选取的。请注意,我嵌套了belongs_to模型的属性,但它也可能以相反的方式工作。

我的工作代码示例:

模型:

class User < ActiveRecord::Base
  belongs_to :account
  accepts_nested_attributes_for :account
  # ...
end

class Account < ActiveRecord::Base
  has_many :users
end

视图:

<h2><%= t(:sign_up) %></h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email %></div>

  <%= f.fields_for :account do |account_form| %>

    <div><%= account_form.label :subdomain %><br />
    <%= account_form.text_field :subdomain %>.<%= request.host %> <span class="hint"></span></div>

  <% end %>

translations_de.yml:

activerecord:

 models:
  account: Konto
  user: Benutzer

 attributes:
  account:
    name: Firmenname
    subdomain: Subdomain
    users: Benutzer

  user:
    # ... no sign of subdomain here ...

视图是使用基于

activerecord.attributes.account.subdomain

Nice 翻译的子域标签呈现的。 :)

我不确定,但它可能需要您使用 activerecord 路径而不是助手路径。

In my Rails 3.2.13 app the attribute labels are picked up automatically from the model whose attributes are embedded. Please note that I am nesting attributes of the belongs_to model, but it might also work the other way around.

My example from working code:

The models:

class User < ActiveRecord::Base
  belongs_to :account
  accepts_nested_attributes_for :account
  # ...
end

class Account < ActiveRecord::Base
  has_many :users
end

The view:

<h2><%= t(:sign_up) %></h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email %></div>

  <%= f.fields_for :account do |account_form| %>

    <div><%= account_form.label :subdomain %><br />
    <%= account_form.text_field :subdomain %>.<%= request.host %> <span class="hint"></span></div>

  <% end %>

translations_de.yml:

activerecord:

 models:
  account: Konto
  user: Benutzer

 attributes:
  account:
    name: Firmenname
    subdomain: Subdomain
    users: Benutzer

  user:
    # ... no sign of subdomain here ...

And the view is rendered with the subdomain label translated based on

activerecord.attributes.account.subdomain

Nice. :)

I'm not sure but it might require you to use the activerecord path instead of the helpers one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文