CanCan - 如何设置无模式控制器?
我有一个用户模型,带有一个管理布尔字段。然后我有一个管理控制器,我想要 CanCan On。
由于没有管理模型,如何将 CanCan 添加到管理控制器?
我已经尝试过:
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
....
但是“访问被零拒绝”的错误
想法?谢谢
I have a User Model, with a admin boolean field. 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?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定您可以访问能力类别中的“current_user”方法吗?我不认为你会这么做。
我记得当使用 CanCan 时,你可以在你的能力类中包含这个:
我希望用户将是当前用户。我想这应该可行。
您还可以查看此页面: https://github.com/ryanb CanCan wiki 的 /cancan/wiki/Non-RESTful-Controllers。这不是完全相同的问题,但由于它涵盖了特殊情况(非 RESTful 控制器),它可能会给您一些关于如何做您想做的事情的提示。
干杯
Are you sure you have access to the 'current_user' method in your ability class? I wouldn't think you would have.
For what I remember when using CanCan you could have this in your Ability class:
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. 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.
Cheers