Jason Whitehorn
Jason Whitehorn

Reputation: 13685

Calling base class method from overloaded method in sub-class

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

Upvotes: 40

Views: 22631

Answers (1)

pythonquick
pythonquick

Reputation: 10849

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

Upvotes: 68

Related Questions