如何从 Rails 3.07 中的过滤器中排除单个控制器操作?

发布于 2024-12-01 21:17:44 字数 222 浏览 1 评论 0原文

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :check_session_expiry, :except => :login

如何排除单个控制器(例如用户控制器)的登录操作?

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :check_session_expiry, :except => :login

How do you exclude a single controller's (the users controller, for example) login action?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁人与我共长歌 2024-12-08 21:17:44

首先,我没有在带有导轨的机器上进行测试,但以下应该可以工作:

class UserController < ApplicationController
  skip_filter :check_session_expiry, :only => :foo

  # following is DEPRECATED as far as I know
  #skip_before_filter :check_session_expiry, :only => :foo

  def foo
  end
end

First, I'm not on a machine with rails to test it, but the following should work:

class UserController < ApplicationController
  skip_filter :check_session_expiry, :only => :foo

  # following is DEPRECATED as far as I know
  #skip_before_filter :check_session_expiry, :only => :foo

  def foo
  end
end
灯角 2024-12-08 21:17:44
class ApplicationController < ActionController::Base
  before_filter :check_session_expiry


  def check_session_expiry
     return true if self.class != UsersController && self.action == "login"
     # do your thing
  end
class ApplicationController < ActionController::Base
  before_filter :check_session_expiry


  def check_session_expiry
     return true if self.class != UsersController && self.action == "login"
     # do your thing
  end
熊抱啵儿 2024-12-08 21:17:44

我只是将控制器中的 check_session_expiry 重新定义为空方法。

  class UserController < ...
    ...
    private
    def check_session_expire
      # optional if other actions shall still use the filter
      super unless self.action == 'login'
    end
  end

I would just redefine check_session_expiry in your controller to be an empty method.

  class UserController < ...
    ...
    private
    def check_session_expire
      # optional if other actions shall still use the filter
      super unless self.action == 'login'
    end
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文