LoginSignup
3
3

More than 3 years have passed since last update.

errors.add(:属性名, "エラーメッセージ") でバリデーションを指定すると、エラーメッセージの先頭に属性名(カラム名)が表示される

Posted at

エラーメッセージの最初に属性(カラム名)が表示されるのは、railsの仕様

errors.add(:属性名, "エラーメッセージ")
で指定すると、エラーメッセージの先頭に属性名が表示される。

errors[:base] << "モデル全体に関係するエラーを追加"
で指定するとモデル全体に関係するエラーを追加することになり、
また、先頭に属性名が表示されることもない。

https://railsguides.jp/active_record_validations.html#errors-add
https://ruby-rails.hatenadiary.com/entry/20140810/1407623400

3
3
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
3
3