【问题标题】:Using CanCan in engines. Ability.rb example在引擎中使用 CanCan。能力.rb 示例
【发布时间】:2012-06-04 17:57:08
【问题描述】:

我是 Rails 开发的新手,我正在尝试创建我的第一个引擎。该引擎可以使用CanCan 来授权和限制用户的权限。

我的引擎中有一些权限,我想在我的主应用程序中继承它们。

例如:

文件 app/models/my_engine/ability.rb 在我的引擎中

module MyEngine
  class Ability
    include CanCan::Ability
    def initialize(user)
      user ||= MyEngine::User.new # guest user
      if user.role? "Admin"
        can :manage, :all
      else
        can :read, :all
      end
    end
  end
end

我的主应用程序中的文件 app/models/ability.rb

class Ability < MyEngine::Ability
  def initialize(user)
    user ||= MyEngine::User.new # guest user
    super(user)
    can :create, SomeModel if user.manager?
  end
end

文件app/controllers/my_engine/application_controller.rb

module MyEngine
  class ApplicationController < ActionController::Base
    def current_ability
       @current_ability ||= MyEngine::Ability.new(current_user)
    end
  end
end

但这不起作用 - 如果我在引擎中使用方法 can? 会收到此错误:

undefined method `can?'

我做错了什么?

【问题讨论】:

    标签: ruby cancan rails-engines


    【解决方案1】:

    关于它的一些讨论在github问题上:ryanb/cancan

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多