Topic: ApplicationController Argument Passing

I have a method in the application controller for authenticating users, and I'm using the before_filter hook in order to call it in my other controllers...

class ApplicationController < ActionController::Base
  def authorize
    unless session[:user_id] and session[:user_password] and session[:user_right
s]
      redirect_to(:controller => "users", :action => "login")
    end
  end
end

before_filter :authorize, :except => :login

I'd like to have a required rights parameter (int) in the class and have the before_filter hook pass it in.  I've tried some things but nothing works.  Symbols are still somewhat of a mystery to me; as is application inheritance.

PS.  Is there a way to filter individual methods in a controller?

Last edited by circuitshaman (2006-07-13 16:07:37)

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)

Re: ApplicationController Argument Passing

Try this:

# In your controller
before_filter(:except => :login) { |controller| controller.authorize(5) }

# in ApplicationController
def authorize(required_rights = 0)
  # Redirect to log-in if user doesn't have the required rights
end


circuitshaman wrote:

PS.  Is there a way to filter individual methods in a controller?

You can use the :only option to do this:

before_filter(:only => :view) { |c| c.authorize(5) }
before_filter(:only => [:edit, :destroy]) { |c| c.authorize(8) }

Is that what you wanted?

Railscasts - Free Ruby on Rails Screencasts

Re: ApplicationController Argument Passing

Perfect!  Thanks for that.

If art interprets our dreams, the computer executes them in the guise of programs.

-SICP (Abelson, Sussman)