Rails 3 way of doing skip_before_filter, :only

RameshVel picture RameshVel · Jul 23, 2011 · Viewed 18.4k times · Source

In rails 2.8 we can write skip_before_filter like this

skip_before_filter :require_login, :only => [:create,:new,:accept]

which means, i wanted to apply the filter require_login only to these actions [:create,:new,:accept], and skip the filter for others.

But it seems, this way is deprecated in rails 3. And new skip_filter is added. i have tried this

 skip_filter :require_login, :only => [:create,:new,:accept]

but its not working, so how can i do this in rails 3.

Answer

Douglas picture Douglas · Sep 7, 2013

That is an incorrect use of skip_before_filter.

In order to apply the filter require_login exclusively to the actions [:create,:new,:accept], and skip the filter for others, you must first apply the filter:

before_filter :require_login

Then you must tell rails to skip this filter except for actions "create, new and accept".

skip_before_filter :require_login, :except => [:create,:new,:accept]

you can also use skip_filter, which allows you to include before_filter, after_filter, and around_filter filters:

skip_filter :require_login, :except => [:create,:new,:accept]

Reference: Rails 3.2 guide

In Rails 4.0, the equivalent methods are :before_action, and :skip_before_action.

Reference: Rails 4.0 guide