5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsでskip_before_actionにてifとonly(except)を併用できない

Posted at

問題

skip_before_action :hoge, only: :method_a, if: -> { condition }

とするとifオプションが無視される。skip_before_actionコールバックに限った話では無さそうだが、検証していないのでここでは一般化しない。claudiob氏のコメントを読むにRails 5で修正されているかもしれない。
一応その時点でのドキュメントには以下のように書いてあった

Note that :only has priority over :if in case they
are used together.

only: :index, if: -> { true } # the :if option will be ignored.

Note that :if has priority over :except in case they
are used together.

except: :index, if: -> { true } # the :except option will be ignored.

回避策

あまり気が進まない方法ではあるが、

skip_before_action :hoge, if: proc { condition && action_name == 'method_a' }

とするというissueを見ているとコメントが見られた。

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?