Specify which controllers to exclude from before_f

2020-02-07 16:53发布

I'm using devise for authentication and have some before_filters in my application controller. Issue I'm seeing is that when I try to logout the before_filter intercepts that and keeps me on the view that's I've setup in the before_filter. Is there any way for me to specify which controllers should be excluded from the application controller or some other file?

4条回答
Animai°情兽
2楼-- · 2020-02-07 17:05

In config/application.rb

config.to_prepare do
  Devise::SessionsController.skip_before_filter :authenticate_user!
end

Referenced by:

How to skip a before_filter for Devise's SessionsController?

查看更多
够拽才男人
3楼-- · 2020-02-07 17:06

You can qualify a filter with :only or :except.

before_filter :filter_name, :except => [:action1, :action2]

Or if the filter (as I now see is the case in your situation) is defined in ApplicationController and you want to bypass it in a subclass controller, you can use a skip_before_filter with the same qualifications in the subclass controller:

skip_before_filter :filter_name, :except => [:action1, :action2]
查看更多
The star\"
4楼-- · 2020-02-07 17:13

Answers above are good except: DEPRECATION WARNING: skip_before_filter is deprecated and will be removed in Rails 5.1. Use skip_before_action instead.

So please use before_action and skip_before_action instead of *-filter.

查看更多
姐就是有狂的资本
5楼-- · 2020-02-07 17:16

In the controller where you want to skip a before filter specified in an inherited controller, you can tell rails to skip the filter

class ApplicationController
  before_filter :authenticate_user!
end

class SessionsController < ApplicationController
  skip_before_filter :authenticate_user!
end
查看更多
登录 后发表回答