LoginSignup
1
1

More than 3 years have passed since last update.

【Rails】エラーメッセージを個別に表示する方法

Posted at

目標

ezgif.com-video-to-gif.gif

開発環境

・Ruby: 2.5.7
・Rails: 5.2.4
・Vagrant: 2.2.7
・VirtualBox: 6.1
・OS: macOS Catalina

前提

下記実装済み。

Slim導入
投稿機能実装

実装

1.application.rbを編集

application.rb
module Bookers2Debug
  class Application < Rails::Application
    config.load_defaults 5.2

    # 追記
    config.action_view.field_error_proc = Proc.new do |html_tag, instance|
      if instance.kind_of?(ActionView::Helpers::Tags::Label)
        html_tag.html_safe
      else
        class_name = instance.object.class.name.underscore
        method_name = instance.instance_variable_get(:@method_name)
        "<div class=\"has-error\">#{html_tag}
          <span class=\"help-block\">
            #{I18n.t("activerecord.attributes.#{class_name}.#{method_name}")}
            #{instance.error_message.first}
          </span>
        </div>".html_safe
      end
    end
  end
end

【解説】

① エラーが出ていない場合はHTMLをそのまま表示する。

if instance.kind_of?(ActionView::Helpers::Tags::Label)
  html_tag.html_safe

② エラーが出ている場合はエラーメッセージをフォームの下に表示する。

else
  class_name = instance.object.class.name.underscore
  method_name = instance.instance_variable_get(:@method_name)
  "<div class=\"has-error\">#{html_tag}
    <span class=\"help-block\">
      #{I18n.t("activerecord.attributes.#{class_name}.#{method_name}")}
      #{instance.error_message.first}
    </span>
  </div>".html_safe

◎ インスタンスのクラス名を変数へ代入

class_name = instance.object.class.name.underscore

◎ インスタンスのメソッド名を変数へ代入

method_name = instance.instance_variable_get(:@method_name)

◎ エラーメッセージ部分のHTMLを作成する。

"<div class=\"has-error\">#{html_tag}
  <span class=\"help-block\">
    #{I18n.t("activerecord.attributes.#{class_name}.#{method_name}")}
    #{instance.error_message.first}
  </span>
</div>".html_safe

「タイトルを入力してください」と表示する場合、
#{I18n.t("activerecord.attributes.#{class_name}.#{method_name}")}が、
「タイトル」にあたる部分で、
#{instance.error_message.first}が、
「を入力してください」にあたる部分になる。

2.エラーメッセージを日本語化

① Gemを導入

Gemfile
# 追記
gem 'rails-i18n'
ターミナル
$ bundle

application.rbを編集

application.rb
module Bookers2Debug
  class Application < Rails::Application
    config.load_defaults 5.2
    config.i18n.default_locale = :ja # 追記

    config.action_view.field_error_proc = Proc.new do |html_tag, instance|
      if instance.kind_of?(ActionView::Helpers::Tags::Label)
        html_tag.html_safe
      else
        class_name = instance.object.class.name.underscore
        method_name = instance.instance_variable_get(:@method_name)
        "<div class=\"has-error\">#{html_tag}
          <span class=\"help-block\">
            #{I18n.t("activerecord.attributes.#{class_name}.#{method_name}")}
            #{instance.error_message.first}
          </span>
        </div>".html_safe
      end
    end
  end
end

ja.ymlを作成し、編集

ターミナル
$ touch config/locales/ja.yml
ja.yml
ja:
  activerecord:
    attributes:
      book:
        title: タイトル
        body: 本文
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1