Help us understand the problem. What is going on with this article?

【Rails5】モデルでbefore_actionでfalse返って来てるのに以降の処理やられちゃう問題

More than 1 year has passed since last update.

throw(:abort)でOK

例えばモデルでerrors配列にエラーの文言を格納し、それをチェックするメソッドをbefore_◯◯◯で設定するとする。

Rails4

その際にRails4では

errors.blank?

的な感じでerrorsの中身があればfalseを返し、これで以降の処理は行わなかった。
しかし、Rails5では異なるようで。。。(泣)

Rails5

Rails5ではコールバックで処理を戻したいときに

throw(:abort) unledd errors.blank?

というふうに throw(:abort)を使う必要があるようです!!

taigamikami
大学生 自分の勉強・メモとしてQiitaに投稿しています。おかしいと思う部分は遠慮なくご指摘いただければと思います。 Ruby/Rails/Swift/iOS/Python
https://taigamikami.github.io/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした