LoginSignup
1
0

More than 3 years have passed since last update.

Railsエラーメッセージのフォーマットを変更したい(先頭にモデルの属性名をつけたくない場合など)

Last updated at Posted at 2019-10-04

発端

  • Nameを入力してください。
  • Emailを入力しください。
  • Passwordを入力してください。

Railsでは上記のように、エラー文の先頭に必ずモデルの属性名が表示されてしまいます。
(今回は、属性名の日本語化が本題ではありません。)
しかし、先頭にモデルの属性名をつけたくない場合エラーメッセージをカスタマイズしたい場合、このままでは不都合です。

解決策

config/locales/ja.yml
ja:
  errors:
    format:
      "%{message}"

config/locales/ja.ymlに上記を追記してください。
デフォルトの属性名が、"'%{attribute}' %{message}"になっているため、
フォーマットをオーバーライドすれば良いようです。
様々、カスタマイズすることも可能ですね!

参考

Rails 6 allows to override the ActiveModel::Errors#full_message format at the model level and at the attribute level

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0