質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.31%
Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

2回答

322閲覧

バリデーションのエラーメッセージを日本語にしたい

tyosu111

総合スコア8

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2023/09/04 07:33

編集2023/09/05 08:15

実現したいこと

新規登録のバリデーションのエラーメッセージを日本語化したい
イメージ説明

該当のソースコード

application.rb

1# Require the gems listed in Gemfile, including any gems 2# you've limited to :test, :development, or :production. 3Bundler.require(*Rails.groups) 4 5module InstructorMatching 6 class Application < Rails::Application 7 # Initialize configuration defaults for originally generated Rails version. 8 config.load_defaults 7.0 9 10 config.i18n.default_locale = :ja 11 12 # Configuration for the application, engines, and railties goes here. 13 # 14 # These settings can be overridden in specific environments using the files 15 # in config/environments, which are processed later. 16 # 17 # config.time_zone = "Central Time (US & Canada)" 18 # config.eager_load_paths << Rails.root.join("extras") 19 20 # Don't generate system test files. 21 config.generators.system_tests = nil 22 end 23end

config/locales/ja.yml

1ja: 2 activerecord: 3 attributes: 4 user: 5 name: "表示名" 6 password: "パスワード" 7 password_confirmation: "パスワード確認" 8 email: "メールアドレス"

erb

1<% if instructor.errors.any? %> 2 <div style="color: red"> 3 <h2><%= pluralize(instructor.errors.count, "error") %> prohibited this instructor from being saved:</h2> 4 5 <ul> 6 <% instructor.errors.each do |error| %> 7 <li><%= error.full_message %></li> 8 <% end %> 9 </ul> 10 </div> 11 <% end %> 12

                                                                                                                                                                                                                                                               

試したこと

https://zenn.dev/machamp/articles/rails-validation-message
こちらの記事を参考にgemのインストールも完了しております。

gem install 'rails-i18n'

こちらのコマンドのみ実行済です。

補足

もしかしたらgemが正しくインストールされていない可能性があります...

Gemfile

1source "https://rubygems.org" 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby "3.2.2" 5 6# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" 7gem "rails", "~> 7.0.5" 8 9# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails] 10gem "sprockets-rails" 11 12# Use mysql as the database for Active Record 13gem "mysql2", "~> 0.5" 14 15# Use the Puma web server [https://github.com/puma/puma] 16gem "puma", "~> 5.0" 17 18# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails] 19gem "importmap-rails" 20 21# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev] 22gem "turbo-rails" 23 24# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev] 25gem "stimulus-rails" 26 27# Build JSON APIs with ease [https://github.com/rails/jbuilder] 28gem "jbuilder" 29 30# Use Redis adapter to run Action Cable in production 31# gem "redis", "~> 4.0" 32 33# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis] 34# gem "kredis" 35 36# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword] 37# gem "bcrypt", "~> 3.1.7" 38 39# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 40gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] 41 42# Reduces boot times through caching; required in config/boot.rb 43gem "bootsnap", require: false 44 45# Use Sass to process CSS 46# gem "sassc-rails" 47 48# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] 49# gem "image_processing", "~> 1.2" 50 51gem "ridgepole" 52 53gem "seed-fu" 54 55gem "sorcery" 56 57gem "administrate" 58 59group :development, :test do 60 # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem 61 gem "debug", platforms: %i[ mri mingw x64_mingw ] 62end 63 64group :development do 65 # Use console on exceptions pages [https://github.com/rails/web-console] 66 gem "web-console" 67 68 # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler] 69 # gem "rack-mini-profiler" 70 71 # Speed up commands on slow machines / big apps [https://github.com/rails/spring] 72 # gem "spring" 73end

gemのインストールのやり方も教えていただけますと幸いです。
よろしくお願い致します。

追加記載

  1. Gemfileにgemの追加をし、bundle installを実行しました。

イメージ説明
2. ja.ymlファイルの作成
イメージ説明
3. application.rbに1行追加
イメージ説明

ターミナルでのエラー内容

イメージ説明

viewのSyntaxerrorの発生

以下一部抜粋しているviewの中身になります
イメージ説明

解決

ja.ymlファイルをこのようにすることで解決できました

ya.yml

1ja: 2 activerecord: 3 attributes: 4 instructor: 5 name: "名前" 6 email: "メールアドレス" 7 password: "パスワード" 8 password_confirmation: "パスワード(確認用)" 9 errors: 10 models: 11 instructor: 12 attributes: 13 name: 14 blank: "を入力してください" 15 email: 16 taken: "は既に存在します" 17 password: 18 too_short: "は %{count} 文字以上で入力してください" 19 password_confirmation: 20 blank: "を入力してください" 21 messages: 22 confirmation: "と%{attribute}が一致しません" 23 24モデルと合わせることが重要でした!! 25

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tyosu111

2023/09/05 00:20

コメントありがとうございます。 Gemfile載せております。ご確認いただけますと幸いです。よろしくお願い致します。
guest

回答2

0

自己解決

質問文の中に解決できたことを記載しております。

投稿2023/09/05 08:16

tyosu111

総合スコア8

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

原因が一つだけかどうかまだきちんと見てませんが、すくなくとも
Gemfile に rails-i18n が無い様です。

投稿2023/09/05 04:11

winterboum

総合スコア23645

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tyosu111

2023/09/05 07:30

コメントありがとうございます!!! Gemfileに記載し、bundle installも実行しております。 また、エラーが発生し、追加で質問文の中に記載させていただきました。ご確認いただけますと幸いです。よろしくお願い致します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.31%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問