gpt4 book ai didi

ruby-on-rails-3 - rails i18n accepts_nested_attributes_for how-to translate?

转载 作者:行者123 更新时间:2023-12-05 00:31:51 25 4
gpt4 key购买 nike

我正在使用 Rails 3.2.6,这是一个示例:

class Man < ActiveRecord::Base
has_many :eyes
accepts_nested_attributes_for :eyes
end
class Eye < ActiveRecord::Base
belongs_to :man
validates_inclusion_of :color, in: { %w[brown green blue] }
end

View (在 HAML 中):
= form_for @man do |f|
- if @man.errors.any?
#error_explanation
%h2= t 'errors.messages.record_invalid', count: @man.errors.count
%ul
- @man.errors.full_messages.each do |msg|
%li= msg

= f.fields_for(:eyes) do |b|
.field
= b.label :color
= b.text_field :color

.actions
= f.submit :submit

它.yml:
it:
activerecord:
attributes:
customer:
eyes: Occhi
customer/eyes:
color: Colore
errors:
models:
man/eyes:
attributes:
color:
inclusion: non valido

但是颜色的标签没有翻译(而是用'actviterecord.attributes.eye.color'),错误信息中的属性只有“Occhi”,其余的是 errors.model.eyes.attributes.color.inclusion而不是 errors.models.man/eyes.attributes.color.inclusion
错误消息是 errors.model.man.attributes.eyes.inclusion ,但我该如何区分呢?它应该类似于“Occhi Colore non valido”而不是“Occhi non valido”

最佳答案

试试这个:

it:
activerecord:
attributes:
# set the name used in nested attribute error messages
customer/eyes:
color: Occhi Colore
errors:
models:
# change the error message for eye color not included in the list
eye:
attributes:
color:
inclusion: non valido
messages:
# change the inclusion message globally
inclusion: non valido
helpers:
label:
# set the label used by form builder for labels
man[eyes_attributes]:
color: Occhi Colore

关于ruby-on-rails-3 - rails i18n accepts_nested_attributes_for how-to translate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14176433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com