From Ruby on Rails error message display to Japanese localization

While studying programming, I implemented the function, so I will leave it as a memorandum. Since it is a beginner, please understand that there may be some mistakes.

How to display the error

Reference material

https://qiita.com/satreu16/items/a072a4be415f30087ed7

Display Rails error message-Qiita

https://note.com/hbkk/n/ndc2c306696b2

https://blog.yuhiisk.com/archive/2018/05/22/rails-display-error-message.html

How to issue Rails error message Validation error --Qiita

Model

post.rb

class Post < ApplicationRecord
  belongs_to :user
	#Input is prohibited only in the content column. Set to allow input only up to 140 characters.
  validates :content, presence: true, length: { maximum: 140 }
end

View

new.html.erb


<%= form_with model: @post, local: true,  html: {class: "form_area"} do |f| %>
        <div class="form_area__field">
					#In case of error layouts/error_Transition to messages,Pass an error message to the transition destination with the name object.
          <%= render 'layouts/error_messages',  object: f.object %>
          <%= f.text_area :content, id: "post_text", placeholder: "Enter the posted content", rows: 10 %>
            <div class="form_area__hidden_field">
              <%= hidden_field :post, :user_id, value: current_user.id %>
            </div>
            <div class="form_area__action">
              <%= f.submit "Post", class: "form_area__action__btn" %>
            </div>
        </div>

error_messages.erb

#Check if error is stored
<% if object.errors.any? %>
    <div class="vali_alert">
      <ul>
				#Get all error messages. Turn with each statement.
        <% object.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

Partializing an error message so that it can be used in other forms is called partial.

Japanese localization of error messages

Reference material

[Beginner] Rails support for Japanese localization by i18n --Qiita

[Ruby on Rails] Japanese notation of error

Install Gem

gem 'rails-i18n'

bundle install implementation

Added to config/application.rb

config.i18n.default_locale = :ja
config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]

Create locales/ja.yml

ja:
  activerecord:
    attributes:
		#Model name you want to set in Japanese
      post:
		#Japanese that you want to correspond to the column name
        content: 'content'

Recommended Posts

From Ruby on Rails error message display to Japanese localization
[Ruby on Rails] How to display error messages
[Rails] Japanese localization of validation error message ~ ja.yml ~
[Ruby on Rails] Individual display of error messages
[Ruby on Rails] How to write enum in Japanese
[Updated from time to time] Ruby on Rails Convenient methods
[Ruby on Rails] From MySQL construction to database change
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Rails] Japanese localization of error messages
From 0 to Ruby on Rails environment construction [macOS] (From Homebrew installation to Rails installation)
How to use Ruby on Rails
Japanese localization of error messages (rails)
How to display a graph in Ruby on Rails (LazyHighChart)
[Rails] Japanese localization of error message when using Form object
Ruby on Rails --From environment construction to simple application development on WSL2
[Ruby on Rails] How to use CarrierWave
[Rails] How to display error messages individually
Deploy to Heroku [Ruby on Rails] Beginner
Preparing to introduce jQuery to Ruby on Rails
[Ruby on Rails] Japanese notation of errors
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] Button to return to top
How to display error messages in Japanese
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
[Error] Switch environment construction to use Ruby on Rails oss (open source)
Deploy to Ruby on Rails Elastic beanstalk (EB deploy)
[Rails] Let's translate the error message into Japanese
How to add / remove Ruby on Rails columns
[Japanese localization] i18n rails Easy Japanese localization view display only
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Ruby on Rails] Ranking display (total, average value)
[Rails] How to convert UC time display to Japanese time display
Ruby on Rails Elementary
Ruby on Rails basics
From Java to Ruby !!
Ruby On Rails Association
[Introduction] Try to create a Ruby on Rails application
Method summary to update multiple columns [Ruby on Rails]
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Ruby on Rails] Change URL id to column name
[Ruby on Rails] Posting score ranking function (whole display)
Deploy to Ruby on Rails Elastic beanstalk (Environment construction)
[Ruby On Rails] How to reset DB in Heroku
[Rails] validates How to translate error sentences into Japanese
(Ruby on Rails6) Reflecting the posted content from the form
(Ruby on Rails6) How to create models and tables
Ruby on Rails Incorrect string value error resolution when posting a form in Japanese
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
Settings to be done when changing from Sublime Text to VScode (when writing Ruby On Rails)
[Ruby on rails + Mysql] Data migration procedure memo when switching from heroku to AWS
[Ruby On Rails] De-root_path! Redirect notation from a nested "child" view to a nested "parent": show
[Note] Error message when using Rails Form object pattern Japanese
What I was interested in in Progate's Ruby on Rails course [Each statement of error message]
Japanese localization of error messages
[Ruby On Rails] How to search and save the data of the parent table from the child table
Japanese localization of error messages
Ruby on rails learning record -2020.10.03
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)