LoginSignup
10
14

More than 5 years have passed since last update.

Ruby on Railsでバリデーションエラーメッセージを個別で取得

Last updated at Posted at 2015-10-20

バリデーションのエラーが発生している状態で

@base=
  #<Hoge:0x007fa48ba76e08
   id: nil,
   text: nil,
   created_at: nil,
   updated_at: nil>,
 @messages=
  {:text=>["を入力してください"]}

全てのエラーメッセージはobj.errors.full_messagesで取得

[11] pry(main)> hoge.errors.full_messages
=> [
 "メッセージを入力してください",
]

個別のエラーメッセージはfull_messages_for(Slymbol)で取得できる

[13] pry(main)> hoge.errors.full_messages_for(:text)
=> ["メッセージを入力してください"]
10
14
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
10
14