fields_forなどで、親モデルにぶら下がった子モデルをフォームで編集し、一括でsave!した場合、
子モデルのバリデーションエラーメッセージが日本語化されません。
object.errors.keys を出力すると、子モデルのエラー対象カラムのattributesは
「"モデルの複数名.カラム名"」
となってしまいます。
config/locals/modelsのja.ymlファイルでは、モデル名は単数系で指定されているため、引っかかりません。
しかし、親モデルにぶら下げずフォーム編集する場合もあるため、複数形の名前でlocalsを定義するわけにもいかず困っております。
何かいい方法はありませんでしょうか。
lang
1class Item < ActiveRecord::Base 2 has_many :order 3 has_many :brand 4end 5 6class Order < ActiveRecord::Base 7 belongs_to :items 8end 9 10class Brand < ActiveRecord::Base 11 belongs_to :brands 12end
lang
1class ItemsController < ApplicationController 2 def update 3 if @item.save! 4 redirect_to 5 else 6 render "edit" 7 end 8 end 9end
app/views/items/edit.html.slim
lang
1= error_messages @item 2= render 'form'
app/helper/application_helper.rb
lang
1module ApplicationHelper 2 def error_messages(*objects) 3 msg = "" 4 objects.each do |o| 5 if o.present? 6 # p o.errors.keys 7 # ex) :"brands.name" 8 # :"orders.quantity" 9 msg += o.errors.full_messages.join('<br />') 10 end 11 end 12 end 13end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。