[Rails] Validation settings and Japanese localization

Validation settings

Describe in the model file. presence: Required for true. Various other settings are possible. Please refer to the official documents for details.

profile.rb


class Profile < ApplicationRecord
  belongs_to :user
  validates :name, presence: true
  validates :age, presence: true
  validates :sex, presence: true
  validates :description, presence: true
  validates :qualify, presence: true
  validates :impression, presence: true
end

Display error message

There are various methods, but this time I used the helper ʻif @ variable name.errors.include?` to do the following. Although only the name is described, the message can be displayed by changing the name to another column name.

new.html.erb


<div class="field">
  <%= f.label :name %><br />
  <%= f.text_field :name, autofocus: true, autocomplete: "name" %>
  
<%#The error content is displayed in red below%>
  <% if @profile.errors.include?(:name) %>
  <p style="color: red;"><%= @profile.errors.full_messages_for(:name).first %>
    <% end %>

When displayed, it looks like the following.

Screen Shot 2020-10-12 at 13.26.32.png

Japanese localization of error message: 1 (message)

This time I will use gem, but Japanese language files are available via Git. If you are interested, please search with rails-i18n. Also, the column names are still in English for this task. Column names will be translated into Japanese in the next paragraph.

gem 'rails-i18n'
bundel install

Japanese localization of error message: 2 (column name)

Create a ja.yml file in the config / locales / models directory. If the indent shifts, it will not work properly, so please refer to the image as well (number of points = indent width).

ja.yml


ja:
  activerecord:
    models:
      profile:profile#Model name with the column you want to translate into Japanese
    attributes:
      profile:
        name:name
        age:age
        sex:sex
        description:Overview
        qualify:Qualification
        impression:enthusiasm

Screen Shot 2020-10-12 at 13.39.23.png

For reference, the contents of validation will be posted again.

profile.rb


class Profile < ApplicationRecord
  belongs_to :user
  validates :name, presence: true
  validates :age, presence: true
  validates :sex, presence: true
  validates :description, presence: true
  validates :qualify, presence: true
  validates :impression, presence: true
end

Japanese localization of error message: 3 (read)

Finally, you need to load the yml file. Add a sentence to config / application.rb as follows. This will allow you to read all the yml files in your directory.

application.rb


module Association03
  class Application < Rails::Application
    config.load_defaults 5.1
    config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.yml').to_s] #Added one sentence

  end
end

Recommended Posts

[Rails] Validation settings and Japanese localization
Rails: Japanese localization of validation messages including devise
[Rails] Japanese localization of validation error message ~ ja.yml ~
[Rails] Japanese localization using rails-i18n
[Rails] Japanese localization of error messages
Japanese localization of error messages (rails)
STS installation and Japanese localization on Windows 10
Japanese localization of CentOS 8 and Japanese keyboard layout
Rails validation and null: false Personal notes
Rails and FormData
[rails] Set validation
[Rails] dependent settings
[Rails5] Rspec -validation-
Validation settings for Ruby on Rails login function
[Japanese localization] i18n rails Easy Japanese localization view display only
Introduction of Rspec and Japanese localization of error messages
Rails valid? And invalid?
Japanese localization of Ubuntu20.04
[Rails] Customize devise validation
[Rails] Custom font settings
Japanese localization of Eclipse
[Rails] Complete routing settings
[Rails] Create initial data with seed.rb [Faker] [Japanese localization]
Spring Tool Suite installation and Japanese localization for Windows
From Ruby on Rails error message display to Japanese localization
A story I was addicted to in Rails validation settings
Options for rails new and settings to be done after rails new
Japanese localization of Docker container of CentOS7 / CentOS8 and Japan time setting
[Rails] Japanese localization of error message when using Form object
Japanese localization of error messages
Docker settings and this and that
Spring validation and error code
Japanese localization of error messages
[Rails] N + 1 problems and countermeasures
Rails Posts and User Linkage
[Rails] require method and permit method
Rails "render method" and "redirect method"
Rails Tutorial Records and Memorandum # 0
rails path and url methods
[Japanese localization of gem: devise]
Rails is difficult and painful!
Signing and validation using java.security.Provider
Introducing Bootstrap and Font-Awesome (Rails)
Rails is difficult and painful! Ⅱ
How to write Rails validation
[Rails] How to use validation
[Rails] strftime this and that
[Rails] Creation of the second vertical axis and Japanese localization in graph drawing using amCharts4, etc.
Rails web server and application server
Ruby on Rails validation summary
[Rails] DB settings and operations (mysql), Rubocop in VSCode, useful extensions
Rails API prevents booleans from being arbitrarily cast and passed validation