Reputation: 1027
I am using Rails 4.
I have two models:
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
And two i18n yaml files:
config/locales/products.ja.yml
ja:
activerecord:
attributes:
product:
name: '名前'
config/locales/product_items.ja.yml
ja:
activerecord:
attributes:
product_item:
description: '説明'
I want to save the two relationship data to database in one form:
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 %>
If the name
or the description
data are not been filled in the form, The errors will been shown.
But only the product data can be changed to Japanese, such as:
名前を入力してください。
The product_item data was showed:
Product_items descriptionを入力してください。
Why the second relationship model not been change to the right i18n?
Upvotes: 2
Views: 209
Reputation: 115541
Have a look at the doc
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:
In your case you should localize under product/product_items
:
ja:
activerecord:
attributes:
product/product_items:
description: "説明"
Upvotes: 3