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.
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.
[Beginner] Rails support for Japanese localization by i18n --Qiita
[Ruby on Rails] Japanese notation of error
gem 'rails-i18n'
bundle install implementation
config.i18n.default_locale = :ja
config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
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