Topic: Temporarily block acces to certain pages

Can anyone suggest a simple way of temporarily blocking access to a set of controllers within my application?

For example, I have a number of controllers in a sub directory foo if I wanted to temporarily divert access from these controllers could I do it using routes.rb?

How can I redirect all attempts to access controllers under foo (i.e. foo/bar) to home?

map.connect 'foo', :controller => 'home'

This works if I try and access but not if I try and access

Or this a better way of doing this without playing with routes.rb?

Last edited by mip (2006-11-09 10:44:51)

Re: Temporarily block acces to certain pages

I would use a before filter:

class FooController < ApplicationController
  before_filter :authorize
  def authorize # may want to rename
    redirect_to :controller => 'home' # probably only redirect on some condition.
    false # IIRC, false needs to be returned in edge rails so the action won't be called

Last edited by ryanb (2006-11-09 12:45:51)

Railscasts - Free Ruby on Rails Screencasts

Re: Temporarily block acces to certain pages

Thanks ryan. As informative as ever. I was rather hoping there'd be something I could use which would mean I wouldn't need to edit the code. That's being lazy though.

This is a nice example of how a before_filter works.