LoginSignup
3
3

More than 3 years have passed since last update.

errorsメソッドは、インスタンスが持つメソッドで、バリデーションがfalseだった時にエラー配列が入る

Posted at

取得するにはmessagesを使う
つまりインスタンス.errors.messagseで取得できる。

例えばコンソールから

>> p = Person.new
# => #<Person id: nil, name: nil>
>> p.errors.messages
# => {}

>> p.valid? ←手動でバリデーション実行
# => false

>> p.errors.messages ←バリデーションでfalseになったので、中にエラーが代入される
# => {name:["空欄にはできません"]}

みたいになる
*Personモデルがnameカラム持ってて、バリデーションに presence: true とかしてた場合。

参考
https://railsguides.jp/active_record_validations.html#valid-questionmark%E3%81%A8invalid-questionmark

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