开发者

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 rai开发者_开发知识库ls 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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜