从子类中的重载方法调用基类方法 [英] Calling base class method from overloaded method in sub-class

查看:45
本文介绍了从子类中的重载方法调用基类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我只是没有使用正确的 Ruby 术语(如果我愿意请纠正我),但 Google 只是没有帮助我解决这个问题.

我拥有的是一个类(称为 OrderController),它扩展了另一个类(称为 BaseStoreController).在 BaseStoreController 中,我定义了一个在整个站点中使用的 before_filter,除了我的 OrderController.在这种非常特殊的情况下,我需要定义一个自定义的 before_filter,它需要执行一些额外的逻辑,然后调用在我的 BaseStoreController 中定义的 before_filter.

我不知道如何做到这一点.

这是我尝试过的,但似乎super"关键字不是我所期望的:

class BaseStoreController <动作控制器::基础before_filter : 授权受保护定义授权#常见的授权逻辑在这里结尾结尾

class OrderController <基础存储控制器before_filter : 授权受保护定义授权#额外的授权逻辑在这里超级授权结尾结尾

我的代码的最终结果是 OrderController 中的授权方法失败并出现以下错误:

<前>当你没想到的时候,你有一个 nil 对象!评估 nil.authorize 时发生错误

解决方案

您是否尝试过仅使用super"而不是来调用基类的authorize"方法super.authorize"?

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

解决方案

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

这篇关于从子类中的重载方法调用基类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆