views:

385

answers:

2

Perhaps I am just not using the correct terminology for Ruby (and if I am please correct me), but Google just isn't helping me on this one.

What I have is a class (call it OrderController) that extends another class (call it BaseStoreController). In the BaseStoreController I have defined a before_filter that is used throughout my site, with the small except of my OrderController. In this very particular situation I needed to define a custom before_filter that needs to do some additional logic and then call the before_filter defined in my BaseStoreController.

What I do not know is how to do this.

Here is what I've tried, but it appears that the 'super' keyword isn't what I was expecting it to be:

class BaseStoreController < ActionController::Base
    before_filter :authorize

    protected
        def authorize
            #common authroization logic here
        end
 end

and

class OrderController < BaseStoreController
    before_filter :authorize

    protected
        def authorize
            #additional authroization logic here
            super.authorize
        end
 end

The end result of my code is that the authorize method in the OrderController is failing with the following error:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.authorize
+6  A: 

Have you tried calling the base class's "authorize" method with just "super" instead of "super.authorize"?

pythonquick
wow!I guess my experience with other languages was harming me on this one... I expected super to be a reference to the base class... not a reference to the base class method I was hiding.That worked like a charm, thanks!
Jason Whitehorn
In Ruby, `super` is a call to the inherited version of the method, so you were calling `authorize` on whatever that returned -- in this case, `nil`.
James A. Rosen
A: 

To make the code a little clearer, you don't need the before_filter line in OrderController.

James Chen