Rails 오류 메시지 표시 및 일본어
오류 메시지 표시
 위의 그림에서 보듯이 제목과 블로그 본문이 비어 있을 때 오류 표시로 설치해야 한다.
모델의 유효성 설정
유효성을 더한 모형(이번은post.rb)에 기재하고 싶습니다.
post.rb
class Post < ApplicationRecord
  validates :title, :content, presence: true
end
title 기둥과 콘텐츠 기둥이 비어 있는 것을 방지합니다.오류 메시지 파일 만들기
오류 메시지 오류 발생 시error_messages.html.erb에 저장합니다.
layouts/_error_messages.html.erb
% if model.errors.any? %>
  <div class="alert alert-warning">
    <ul>
      <% model.errors.full_messages.each do |message| %>
        <li><%= message %></li> 
      <% end %>
    </ul>
  </div>
<% end %>
창 (form with) 에render로 삽입
posts/new.html.erb
<%= form_with model: @post, class: :form, local: true do |form| %>
 #renderメソッドで_error_messages.html.erbを呼び出す。
  <%= render 'layouts/error_messages', model: form.object %> 
  <%= form.text_field :title, placeholder: :タイトル, class: :form__title %>
  <%= form.text_area :content, placeholder: :ブログ本文, class: :form__text %>
  <%= form.submit '投稿する', class: :form__btn %>
<% end %>
이렇게 표시하면 완성됩니다.다음은 일본어화의 실현이다.
오류 메시지의 일본어화
Gemfile
gem 'rails-i18n'
Gemfile에 다음 구절이 추가되었습니다. bundle install.일본어화된 기초 파일 만들기
Rails의 다국어는 yml 파일에서 관리해야 합니다.
디렉터리 바로 아래의 ja.제작 yml.
단말기
$ touch config/locales/ja.yml
열 이름의 일본어
config/locales/models/ja.yml
ja:
  activerecord:
    attributes:
      post:
        title: 名前
        content: ブログ本文
ja.yml 고려 사항
왜냐하면 Rails는 yml 파일의 줄 바꾸기와 들여쓰기에서 일본어화된 경로를 참고했기 때문이다
열 이름의 순서에 따라 줄 바꾸기와 들여쓰기를 넣어야 합니다.
config/locales/models/ja.yml
attributes:         # attributes:の直下に
  post:            # モデル名を指定し
    title: 名前        # カラム名を指定する。
   content:ブログ本文    # カラム名を指定する。
                Reference
이 문제에 관하여(Rails 오류 메시지 표시 및 일본어), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/satreu16/items/a072a4be415f30087ed7텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)