如何使用两个关系 i18n yaml 文件以 Rails 的形式很好地显示?
How to use two relationship i18n yaml file to show well in one form with Rails?
我正在使用 Rails 4.
我有两个模型:
app/models/product.rb
class Product < ActiveRecord::Base
has_many :product_items, foreign_key: :product_id, :dependent => :destroy
accepts_nested_attributes_for :product_items
validates :name, presence: true
end
app/models/product_item.rb
class ProductItem < ActiveRecord::Base
belongs_to :product
validates :description, presence: true
end
还有两个 i18n yaml 文件:
config/locales/products.ja.yml
ja:
activerecord:
attributes:
product:
name: '名前'
config/locales/product_items.ja.yml
ja:
activerecord:
attributes:
product_item:
description: '説明'
我想将两个关系数据以一种形式保存到数据库中:
app/views/products/_form.html.erb
<%= form_for(product, url: path) do |f| %>
<% if f.object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.fields_for :product_items do |item_form| %>
<%= item_form.label :description %>
<%= item_form.text_field :description %>
<% end %>
<%= f.submit 'Submit' %>
<% end %>
如果 name
或 description
数据未填写在表格中,将显示错误。
但是只有商品数据可以改成日文,如:
名前を入力してください。
- 表示:请输入姓名。
显示product_item数据:
Product_items descriptionを入力してください。
- 意思是:请输入描述。
为什么第二个关系模型没有改成正确的国际化?
In the event you need to access nested attributes within a given
model, you should nest these under model/attribute at the model level
of your translation file:
在您的情况下,您应该在 product/product_items
:
下进行本地化
ja:
activerecord:
attributes:
product/product_items:
description: "説明"
我正在使用 Rails 4.
我有两个模型:
app/models/product.rb
class Product < ActiveRecord::Base
has_many :product_items, foreign_key: :product_id, :dependent => :destroy
accepts_nested_attributes_for :product_items
validates :name, presence: true
end
app/models/product_item.rb
class ProductItem < ActiveRecord::Base
belongs_to :product
validates :description, presence: true
end
还有两个 i18n yaml 文件:
config/locales/products.ja.yml
ja:
activerecord:
attributes:
product:
name: '名前'
config/locales/product_items.ja.yml
ja:
activerecord:
attributes:
product_item:
description: '説明'
我想将两个关系数据以一种形式保存到数据库中:
app/views/products/_form.html.erb
<%= form_for(product, url: path) do |f| %>
<% if f.object.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
<ul>
<% object.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
</div>
<% end %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.fields_for :product_items do |item_form| %>
<%= item_form.label :description %>
<%= item_form.text_field :description %>
<% end %>
<%= f.submit 'Submit' %>
<% end %>
如果 name
或 description
数据未填写在表格中,将显示错误。
但是只有商品数据可以改成日文,如:
名前を入力してください。
- 表示:请输入姓名。
显示product_item数据:
Product_items descriptionを入力してください。
- 意思是:请输入描述。
为什么第二个关系模型没有改成正确的国际化?
In the event you need to access nested attributes within a given model, you should nest these under model/attribute at the model level of your translation file:
在您的情况下,您应该在 product/product_items
:
ja:
activerecord:
attributes:
product/product_items:
description: "説明"