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

发布于 2024-10-31 10:20:58 字数 460 浏览 6 评论 0原文

我有一个用户模型,带有一个管理布尔字段。然后我有一个管理控制器,我想要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧瑾黎汐 2024-11-07 10:20:58

您确定您可以访问能力类别中的“current_user”方法吗?我不认为你会这么做。

我记得当使用 CanCan 时,你可以在你的能力类中包含这个:

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

我希望用户将是当前用户。我想这应该可行。

您还可以查看此页面: 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:

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. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文