0
PT-BR.yml:導軌5 - 從嵌套模型翻譯錯誤
pt-BR:
activerecord:
models:
user: Usuário
project: Projeto
attributes:
user:
name: O nome
description: A descrição
projects: Os projetos
project:
name: O nome
errors:
format: "%{attribute} %{message}"
messages:
accepted: deve ser aceito
blank: não pode ficar em branco
........
型號:
class User < ApplicationRecord
has_many :projects, dependent: :destroy, inverse_of: :user
accepts_nested_attributes_for :projects, allow_destroy: true
end
class Project < ApplicationRecord
belongs_to :user, inverse_of: :projects
validates :name, presence: true, length: { mininum: 3, maximum: 255 }
end
控制器:
def update
if @user.update(user_params)
render json: @user
else
render json: { errors: @user.errors.full_messages }, status: :unprocessable_entity
end
end
所有錯誤當方法.error.full_messages
被調用時,正在翻譯消息,但那些來自項目的項目對象(正如您可能會注意到的那樣,我正在使用accepts_nested_attributes_for
)。
我總是收到以下錯誤信息:
{
"errors": [
"Projects name deve conter no mínimo 3 caracteres"
]
}
我怎麼能在我的翻譯文件翻譯項目和名甚至刪除「專案%屬性附加傷害%」,從錯誤消息(沒有任何黑客)?
謝謝你的回答...但是,我真的不認爲我需要再次添加**所有**錯誤*鍵下......因爲,正如我所說,*所有的錯誤信息正在翻譯當調用.error.full_messages方法時,除了來自項目對象*的方法之外。所以,我試圖找到一種方法來翻譯它們或者刪除。我正在尋找這樣的[**問題**](http://stackoverflow.com/questions/5941856/suppress-base-in-error-text-for-custom-validation-of-rails-nested-屬性)(但它不適用於我 - 也許是因爲它太舊了)。 –