[Ruby on Rails] How to display error messages

Define validation

Define in the model.

class User < ApplicationRecord
  validates :name, {presence: true}
  validates :email, {presence: true}
end

In the above, we have restricted empty posts for name and email.

Display error message in view

When an error occurs, the error message will be put in an array in error.full_message.

<% if @user.errors.any? %>
  <% @user.errors.full_messages.each do |message| %>
    <%= message %>
  <% end %>  
<% end %>

If there is a @ user error, all @ user.errors.full_messages will be output.

Make the error message Japanese

The error message is in English by default, so let's translate it into Japanese.

Added to Gem file

Add the following to the Gem file and do bundle install.

 gem 'rails-i18n'

Added to config/application.rb

Add the following to config/application.rb and restart the server.

config.i18n.default_locale = :ja

Japaneseization of user columns

As it is, the value part of the error message is not translated into Japanese as shown below.

["Please enter name"]

Create a file called ja.yml in config/locales/models/. Add the following to the ja.yml file.

ja:
  activerecord:
    
    attributes:
      user:
        name:name
       email:Email

Finally, add a sentence to read this yml file to config/application.rb.

config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.yml').to_s]

done.

["Please enter your name"]

Recommended Posts

[Ruby on Rails] How to display error messages
[Rails] How to display error messages individually
[Ruby on Rails] Individual display of error messages
How to use Ruby on Rails
[Ruby on Rails] How to use CarrierWave
From Ruby on Rails error message display to Japanese localization
How to display a graph in Ruby on Rails (LazyHighChart)
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Rails] How to display error messages for comment function (for beginners)
How to display error messages in Japanese
How to add / remove Ruby on Rails columns
[Ruby on Rails] How to install Bootstrap in Rails
[Rails] How to get success and error messages
[Ruby on Rails] How to use session method
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails] How to change the column name
[Ruby On Rails] How to reset DB in Heroku
(Ruby on Rails6) How to create models and tables
How to deploy jQuery on Rails
[Rails] How to use flash messages
[rails] How to display db information
[Ruby on Rails] Add and delete tags and display (success / error) messages using ajax.
How to deploy Bootstrap on Rails
[Rails] Display form error messages asynchronously
Rails on Tiles (how to write)
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
Deploy to Heroku [Ruby on Rails] Beginner
Preparing to introduce jQuery to Ruby on Rails
[Rails] How to use video_tag to display videos
[Ruby on Rails] Button to return to top
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Ruby on Rails] How to avoid creating unnecessary routes for devise
How to find the cause of the Ruby error
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
About Ruby error messages
The road to Japaneseizing Rails devise error messages
How to write Rails
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to display error messages and success messages when registering as a user
Ruby on Rails Elementary
How to uninstall Rails
How to implement gem "summer note" in wysiwyg editor in Ruby on Rails
Ruby on Rails basics
How to build a Ruby on Rails development environment with Docker (Rails 6.x)
[Error] Switch environment construction to use Ruby on Rails oss (open source)
[Rails MySQL] How to reset DB on heroku
How to solve the local environment construction of Ruby on Rails (MAC)!
[Rails] How to solve "Uglifier :: Error: Unexpected character'`'"
Ruby On Rails Association
How to debug the processing in the Ruby on Rails model only on the console
How to build a Ruby on Rails development environment with Docker (Rails 5.x)
[Ruby On Rails] How to search the contents of params using include?
[Ruby on Rails] Ranking display (total, average value)
[Rails] How to convert UC time display to Japanese time display
[Ruby on Rails] How to make the link destination part of the specified id
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
How to implement image posting function using Active Storage in Ruby on Rails