R – How tomplement a Redirect on All Requests (on certain conditions)

redirectrubyruby-on-rails

I want to set something up so that if an Account within my app is disabled, I want all requests to be redirected to a "disabled" message.

I've set this up in my 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

Of course, this doesn't quite work as it goes into an infinite loop if the Account is not active. I was hoping to use something like:

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

but I noticed that in Rails v2.1 (what I'm using), @controller is now controller and this doesn't seem to work in ApplicationController.

What would be the best way to implement something like this?

Best Solution

You could also use a skip_before_filter for the one controller/method you don't want to have the filter apply to.