How to implement redirection for all requests (under certain conditions)?

I want to configure something so that if the account in my application is disabled, I want all requests to be redirected to the message "disabled".

I installed this in my ApplicationController application:

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 does not work, because it goes into an endless loop if the Account is inactive. 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 use), @controller is now a controller, and this does not seem to work in ApplicationController.

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

+3
source share
4 answers

skip_before_filter /, .

+3

.

"" , before_filter, :

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

, , MainController.rb:

  def check_account
    return if action_name == "disabled"
    super
  end
+6

, $account. , . @ , ApplicationController current_account, @current_account.

+1
source

If you don't override too much, then just put the if in the redirect filter

if action! = disabled redirection () end

0
source

Source: https://habr.com/ru/post/1697094/


All Articles