FastSolutions
FastSolutions

Reputation: 1829

Redirect all pages except 1 to a single Temporary Unavailable Page in Rails

We have a fully functional website but the backend is going down for maintenance, so we want to redirect all our customers to www.example.com/unavailable. Except for 1 page www.example.com/admin since this is the admin page where we put the unavailable page on and off.

So my question: Is it possible to turn routes on and off like this:

Example code for routes.rb:

  if Settings.unavailable
    get "*", to: "/unavailable", except: "/admin"
  end

Upvotes: 0

Views: 777

Answers (2)

FastSolutions
FastSolutions

Reputation: 1829

With the help from the post from Michal, I've adapted his answer to the following:

class ApplicationController < ActionController::Base
  before_filter :check_maintenance

  def check_maintenance
    if Settings.first.maintenance && request.fullpath.split("?")[0].gsub("/","") != 'unavailable' && (request.fullpath.split("?")[0].exclude? "admin") # or other conditions
      redirect_to '/unavailable'
    end
  end
end

Upvotes: 1

Michał Młoźniak
Michał Młoźniak

Reputation: 5556

You can put conditions in your routes.rb but you won't be able to detect what is the current url. You should put this logic in before_filter in ApplicationController (or other top level controller):

class ApplicationController < ActionController::Base
  before_filter :check_maintenance

  def check_maintenance
    if Settings.unavailable && action_name != 'unavailable' # or other conditions
      redirect_to '/unavailable'
    end
  end
end

Upvotes: 0

Related Questions