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

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

More than 3 years have passed since last update.

問題

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を見ているとコメントが見られた。

参考

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