简体   繁体   English

CanCan-如何设置无模式控制器?

[英]CanCan - How to setup a modeless Controller?

I have a User Model, with a admin boolean field. 我有一个用户模型,带有admin布尔字段。 I then have an Admin Controller which I want CanCan On. 然后,我有一个我想可以打开的管理控制器。

How do I add CanCan to the Admin Controller as there is not Admin Model? 由于没有管理模型,如何将CanCan添加到管理控制器?

I've tried: 我试过了:

class AdminController < ApplicationController
  load_and_authorize_resource :user
  load_and_authorize_resource :admin, :through => :user


class Ability
   include CanCan::Ability
   def initialize
         ....
         can :manage, Admin if current_user.admin == TRUE
         ....

But that errors with "Access denied on nil" 但是,该错误与“访问被拒绝为零”

Ideas? 有想法吗? Thanks 谢谢

Are you sure you have access to the 'current_user' method in your ability class? 您确定您可以访问能力类中的“ current_user”方法吗? I wouldn't think you would have. 我想你不会。

For what I remember when using CanCan you could have this in your Ability class: 我记得使用CanCan时可以在Ability类中使用:

def initialize(user)
    user ||= User.new #guest user.
...
end

And the user would be the current user, I expect. 我希望该用户将是当前用户。 And I guess that should work. 我想应该可以。

You can also take a look at this page: https://github.com/ryanb/cancan/wiki/Non-RESTful-Controllers of the CanCan wiki. 您也可以查看以下页面:CanCan Wiki的https://github.com/ryanb/cancan/wiki/Non-RESTful-Controllers It's not exactly the same issue, but as it covers a special case (Non-RESTful-Controllers) it might give you some hints about how to do what you want to do. 这不是完全相同的问题,但是由于它涵盖了特殊情况(非RESTful控制器),因此可能会给您一些有关如何做您想做的事情的提示。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM