[Rails] How to display error messages individually

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

Recommended Posts

[Rails] How to display error messages individually
How to display error messages in Japanese
[Rails] How to display error messages for comment function (for beginners)
[Rails] How to get success and error messages
[Rails] How to use flash messages
[rails] How to display db information
[Rails] Display form error messages asynchronously
[Rails] How to use video_tag to display videos
How to write Rails
How to uninstall Rails
The road to Japaneseizing Rails devise error messages
[Rails] How to solve "Uglifier :: Error: Unexpected character'`'"
[Rails] How to convert UC time display to Japanese time display
[Ruby on Rails] Individual display of error messages
How to display error messages and success messages when registering as a user
[rails] How to post images
How to set the display time to Japan time in Rails
[Rails] How to use enum
[Rails, JS] How to implement asynchronous display of comments
[Rails] How to install devise
[Rails] How to use enum
How to read rails routes
[Java] How to display Wingdings
[Rails] validates How to translate error sentences into Japanese
How to use rails join
Display Flash messages in Rails
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[RSpec] How to test error messages set by Shoulda-Matchers
[Rails] How to display an image in the view
[Rails] How to use Scope
From Ruby on Rails error message display to Japanese localization
How to display a graph in Ruby on Rails (LazyHighChart)
[Rails] How to display the list of posts by category
[Rails] How to display information stored in the database in view
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
[Rails] How to write in Japanese
[Rails] How to prevent screen transition
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
[Rails] How to add new pages
Rails on Tiles (how to write)
[Rails] How to write exception handling?
Japanese localization of error messages (rails)
[Rails] How to install ImageMagick (RMajick)
[Rails] How to install Font Awesome
[Rails] How to use Active Storage