重写ruby中的类方法没有按预期工作

时间:2012-11-22 13:04:38

标签: ruby-on-rails ruby ruby-on-rails-3 oop cancan

我有一个基本控制器来干掉很多我的沼泽标准crud控制器,我的想法是,如果我需要做一些非标准的事情,我只会覆盖每个方法。

class MyBaseController < ApplicationController

  load_and_authorize_resource

  # Common crud actions

end

现在我的一个子控制器中有一个自定义操作(export_csv)

class ReportsController < MyBaseController 

  load_and_authorize_resource :except => :export_csv

  def export_csv
    # custom auth and other stuff
  end

end

现在我无法再点击我的export_csv操作了,而是获得了访问被拒绝的异常。如果我不从我的基本控制器继承,一切都很好。

我以为我可以按照以下方式解决这个问题:

class MyBaseController < ApplicationController

  load_and_authorize_resource :except => auth_exceptions

  def self.auth_exceptions
    []
  end

  # Common crud actions

end

然后根据需要覆盖我的子类中的self.auth_exceptions。

然而,这会触发错误“MyBaseController的未定义局部变量或方法`auth_exceptions':Class”

有没有人有任何建议选择性地覆盖我的子控制器中的加载和授权资源?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

尝试使用派生类:

skip_load_and_authorize_resource :only => :export_csv

Doc在这里:skip_load_and_authorize_resource

相关问题