Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 way of doing skip_before_filter, :only

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.

like image 368
RameshVel Avatar asked Jul 23 '11 13:07

RameshVel


2 Answers

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

like image 156
Douglas Avatar answered Nov 08 '22 23:11

Douglas


  • skip_before_filter has not been deprecated, see source.

  • You're using it as expected.

So how did you deduce it's not working properly?

like image 11
apneadiving Avatar answered Nov 08 '22 21:11

apneadiving