4

使用: 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]

前もって感謝します

4

2 に答える 2

4

私はこれをしました:

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'
于 2011-02-01T19:53:53.783 に答える
0

私の Rails 3.2.13 アプリでは、属性が埋め込まれているモデルから属性ラベルが自動的に取得されます。私は属しているモデルの属性を入れ子にしていることに注意してください。

作業コードからの私の例:

モデル:

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

良い。:)

よくわかりませんが、ヘルパーの代わりにアクティブレコードのパスを使用する必要があるかもしれません。

于 2013-06-05T18:23:20.260 に答える