LoginSignup
1
0

More than 5 years have passed since last update.

flash エラーメッセージ 特定カラムのメッセージだけを取り出す方法

Posted at

起きた事象

controllerにおいて例外処理をしているとき

raise  ActiveRecord::RecordInvalid
flash.now[:alert] = @takumi.errors.full_messages
render :new

=> 例外処理をしているのに、すべてのエラーが取れてしまうため例外が特定できない

解決策

full_messages_for(:symbol)を使うといい感じに取り出せるヨ

@messages=
  {:first_name=>["を入力してください"]}

[1] pry(main)> takumi.errors.full_messages
=> ["first_nameを入力してください",
    "性別を入力してください"
]

[2] pry(main)> takumi.errors.full_messages_for(:first_name)
=> ["first_nameを入力してください"]

結果

raise  ActiveRecord::RecordInvalid
flash.now[:alert] = @takumi.errors.full_message_for(:first_name)
render :new

参考
API dock

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