views:

94

answers:

2

I have a method in my ApplicationController that is part of a before_filter. How do I identify which controller is calling that method and can I pass arguments to it?

Presumably worst case, I can create a new object where I use controller names and values, then call it directly in the before_filter method with NewObject.find(:first, :conditions => ['controller_name = ?', controller_name], but that smells very bad.

So I'm open to ideas. Thanks in advance.

psuedo-short-code:

class ApplicationController < ActionController::Base
    before_filter :someMethod
    ....
    def someMethod
        Do stuff
    end


class SomeController < ApplicationController
    # presumably the before_filter runs here
    @someValueIWantToPass = some.value
    ...
+1  A: 

Using self.class will tell you which controller has called the before_filter.

class HomeController < ApplicationController
  before_filter :awesome

  def index
    render :text => @blah
  end

  def awesome
    @blah = self.class
  end
end

will render out "HomeController"

Corban Brook
+5  A: 

params[:controller] and params[:action] contain the controller and action requested, and are available from inside a filter.

Misplaced