前提・実現したいこと
Rails3からRails5へのバージョンアップの過程で、下記エラーが出ます:
undefined method `setting.user' for #<User:0x00007f65e23febd0>
試したこと等
i18n
のモデル名とモデル属性値からymlファイルを参照するメソッド群で、適切な文字列処理が行えていないと考え、下記errors_i18n.rb
のconver_name
メソッドをいろいろいじってみたのですが、エラー解消には至りませんでした。
該当のソースコード
config/initializer/errors_i18n.rb
ruby
1 def full_messages 2 full_messages = [] 3 4 each do |attribute, messages| 5 messages = Array.wrap(messages) 6 next if messages.empty? 7 8 if attribute == :base 9 messages.each {|m| full_messages << m } 10 else 11 # attr_name = attribute.to_s.gsub('.', '_').humanize 12 attr_name = convert_name(@base, attribute.to_s) 13 attr_name ||= attribute.to_s.gsub('.', '_').humanize 14 attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) 15 options = { :default => "%{attribute} %{message}", :attribute => attr_name } 16 17 messages.each do |m| 18 full_messages << I18n.t(:"errors.format", options.merge(:message => m)) 19 end 20 end 21 end 22 23 full_messages 24 end 25 26 def convert_name(model, name) 27 default = nil 28 if name =~ /(.+).(.+)/ 29 base_name = $1 30 attribute = $2 31# base = ActiveSupport::Dependencies.constantize(base_name.camelize) 32 base = model.__send__(base_name).class 33 if (base.respond_to? :model_name) 34 default = "#{base.human_attribute_name(attribute.to_sym, :default => attribute.humanize)}" 35 end 36 end 37 default 38 end
ja.yml
(抜粋)
ruby
1errors: 2 format: "%{attribute}%{message}" 3 4 messages: &errors_messages 5 inclusion: "は一覧にありません。" 6 exclusion: "は予約されています。" 7 invalid: "は不正な値です。" 8 confirmation: "が一致しません。" 9 accepted: "を受諾してください。" 10 empty: "を入力してください。" 11 blank: "を入力してください。" 12 too_long: "は%{count}文字以内で入力してください。" 13 too_short: "は%{count}文字以上で入力してください。" 14 wrong_length: "は%{count}文字で入力してください。" 15 not_a_number: "は数値で入力してください。" 16 not_an_integer: "は整数で入力してください。" 17 greater_than: "は%{count}より大きい値にしてください。" 18 greater_than_or_equal_to: "は%{count}以上の値にしてください。" 19 equal_to: "は%{count}にしてください。" 20 less_than: "は%{count}より小さい値にしてください。" 21 less_than_or_equal_to: "は%{count}以下の値にしてください。" 22 odd: "は奇数にしてください。" 23 even: "は偶数にしてください。" 24 not_future: "は未来の時間を入力してください。" 25 ends_space: "は半角スペースが含まれています。" 26 27 fail_get_data: "データを正常に取得できませんでした。" 28 29 activerecord: 30 errors: 31 template: 32 header: 33 one: "%{model}にエラーが発生しました。" 34 other: "%{model}に%{count}つのエラーが発生しました。" 35 body: "次の項目を確認してください。"
translation_ja.yml
ruby
1 user: 2 email: "メールアドレス" 3 encrypted_password: "暗号化パスワード" 4 password_salt: "パスワードのソルト" 5 reset_password_token: "パスワードのリセットトークン" 6 (略) 7 user_setting: 8 name: 氏名 9 prefecture_id: 住所(都道府県)
補足情報(FW/ツールのバージョンなど)
- Ruby 2.6.5
- Rails 5.2.0
- i178n 1.7.0
このエラーメッセージから、I18nを疑うのがなぜなのか疑問です。
どのfileの土の行で起きていますか?
あなたの回答
tips
プレビュー