Internationalization 中嵌套的属性的Rails 3 I18n标签转换有很多关系
使用:Rails 3.0.3、Ruby 1.9.2 这是关系:Internationalization 中嵌套的属性的Rails 3 I18n标签转换有很多关系,internationalization,ruby-on-rails-3,nested-attributes,Internationalization,Ruby On Rails 3,Nested Attributes,使用: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 我也尝试过
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
我也尝试过:
person[contact_method_attributes]
这对于1-1关系来说非常有效,例如
person[wife_attributes]:
name: 'My wife'
但不是个人[妻子属性]
提前感谢我是通过以下方式做到这一点的:
en:
helpers:
label:
person[contact_methods_attributes][0]:
info: 'First custom label here'
person[contact_methods_attributes][1]:
info: 'Second custom label here'
这很好,但当你有无限的选择时并不理想。。我只想在表单生成器中指定一个自定义翻译键:)
在我的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
并使用基于转换的子域标签渲染视图
activerecord.attributes.account.subdomain
不错。:)
我不确定,但它可能要求您使用activerecord路径而不是helpers路径。在Rails 3.2.12中,我无法获取:'helpers.label.person.contact_method.info'来工作,但是:'helpers.label.person[contact_method_attributes].info'可以。在Rails 3.1.10中,我可以使用“helpers.label.person.contact\u methods.info”来工作,但必须使用“helpers.label.person[contact\u methods\u attributes][new\u contact\u methods]”来实现我的javascript可添加的多个关联。(当为向DOM添加字段的my按钮的data content属性呈现模板时,“new_contact_methods”是id的占位符。)请注意,如果对象尚不存在(
account_form.object
为nil
),Rails将不会在模型中查找翻译。
en:
helpers:
label:
person:
contact_methods:
info: 'Custom label here'
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 %>
activerecord:
models:
account: Konto
user: Benutzer
attributes:
account:
name: Firmenname
subdomain: Subdomain
users: Benutzer
user:
# ... no sign of subdomain here ...
activerecord.attributes.account.subdomain