[Ruby] [Rails] How to display error messages individually

1 minute read

Target

ezgif.com-video-to-gif.gif

Development environment

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

Premise

The following has been implemented.

Slim introduction
Implementation of posting function

Implementation

1. Edit ʻapplication.rb`

application.rb


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

    #Postscript
    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

[Explanation]

(1) If no error occurs, HTML is displayed as it is.

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

(2) If there is an error, display the error message at the bottom of the form.

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

** ◎ Assign the instance class name to a variable **

class_name = instance.object.class.name.underscore

** ◎ Assign the method name of the instance to a variable **

method_name = instance.instance_variable_get(:@method_name)

** ◎ Create HTML for the error message part. ** **

"<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

If “Please enter a title” is displayed,
# {I18n.t ("activerecord.attributes. # {Class_name}. # {Method_name} ")}
In the part corresponding to the “title”
# {Instance.error_message.first} is
It becomes the part corresponding to “Please enter”.

2. Japaneseize error messages

① Introduce Gem

Gemfile


#Postscript
gem 'rails-i18n'

Terminal


$ bundle

② Edit ʻapplication.rb`

application.rb


module Bookers2Debug
  class Application < Rails::Application
    config.load_defaults 5.2
    config.i18n.default_locale = :ja #Postscript

    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

③ Create and edit ja.yml

Terminal


$ touch config/locales/ja.yml

ja.yml


ja:
  activerecord:
    attributes:
      book:
        title:title
        body:Text