代码之家  ›  专栏  ›  技术社区  ›  Dan Harper

如何在所有请求上实现重定向(在特定条件下)?

  •  3
  • Dan Harper  · 技术社区  · 16 年前

    我想设置一些内容,以便在应用程序中的某个帐户被禁用时,将所有请求重定向到“禁用”消息。

    我已在ApplicationController中设置此项:

    class ApplicationController < ActionController::Base
      before_filter :check_account
    
      def check_account
        redirect_to :controller => "main", :action => "disabled" and return if !$account.active?
      end
    end
    

    当然,如果帐户没有激活,这不会很好地工作,因为它会进入一个无限循环。我希望使用类似的东西:

    redirect_to :controller => "main", :action => "disabled" and return if !$account.active? && @controller.controller_name != "main" && @controller.action_name != "disabled"
    

    但我注意到在Rails2.1(我使用的)中,@controller现在是控制器,而在applicationcontroller中似乎不起作用。

    什么是实现此类功能的最佳方法?

    4 回复  |  直到 16 年前
        1
  •  3
  •   Tony Pitale    16 年前

    skip_before_filter

        2
  •  6
  •   Ian Terrell    16 年前

    before_filter :check_account, :except => :disabled
    

      def check_account
        return if self.controller_name == "main" && self.action_name == "disabled"
    
        redirect_to :controller => "main", :action => "disabled" and return if !$account.active?
      end
    

      def check_account
        return if action_name == "disabled"
        super
      end
    
        3
  •  1
  •   Eric Allam    16 年前

        4
  •  0
  •   flukus    16 年前