CanCan - 查找特定类的未定义方法

发布于 2024-12-21 15:05:40 字数 1144 浏览 0 评论 0原文

我有一个名为管理员的控制器。该控制器对其他模型有操作。当我尝试使用 CanCan 授权此操作时,我收到此响应。

<h1>
  NoMethodError
    in AdministratorController#preproduct_delete
</h1>
<pre>undefined method `find' for Administrator:Class</pre>

控制器代码开头为:

class AdministratorController < ApplicationController

  load_and_authorize_resource
  before_filter :authenticate_user!

  def users
    authorize! :users, "Users List"
    @users = User.all
  end

end

能力模型具有:

if user.admin? then :admin
    can :users
end

authenticate_user! 方法:

# See ControllerAdditions#authorize! for documentation. 
def authorize!(action, subject, *args) 
  message = nil 
  if args.last.kind_of?(Hash) && args.last.has_key?(:message) 
    message = args.pop[:message] 
  end 
  if cannot?(action, subject, *args) 
    message ||= unauthorized_message(action, subject) 
    raise AccessDenied.new(message, action, subject) 
  end 
  subject 
end 

我尝试从控制器中删除 load_and_authorize_resource,但是当调用任何操作时,CanCan 每次都会将我重定向到登录页面。

多谢

I have a controller named Administrator. This controller has actions to anothers models. When I try to authorize this actions with CanCan I get this response.

<h1>
  NoMethodError
    in AdministratorController#preproduct_delete
</h1>
<pre>undefined method `find' for Administrator:Class</pre>

The controller code begins with:

class AdministratorController < ApplicationController

  load_and_authorize_resource
  before_filter :authenticate_user!

  def users
    authorize! :users, "Users List"
    @users = User.all
  end

end

The ability model has:

if user.admin? then :admin
    can :users
end

The authenticate_user! method:

# See ControllerAdditions#authorize! for documentation. 
def authorize!(action, subject, *args) 
  message = nil 
  if args.last.kind_of?(Hash) && args.last.has_key?(:message) 
    message = args.pop[:message] 
  end 
  if cannot?(action, subject, *args) 
    message ||= unauthorized_message(action, subject) 
    raise AccessDenied.new(message, action, subject) 
  end 
  subject 
end 

I tried to remove load_and_authorize_resource from the controller, but when any action is called, CanCan redirects me to login page every time.

Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

萤火眠眠 2024-12-28 15:05:41

您没有指定权限, can :user 是什么意思?指定一个权限,所有权限使用:manage。

if user.admin?
    can :manage, :users
end

You did not specified the permission, what does can :user mean ? specify a permission, for all permissions use :manage.

if user.admin?
    can :manage, :users
end
在你怀里撒娇 2024-12-28 15:05:41
<h1>
  NoMethodError
    in AdministratorController#preproduct_delete
</h1>

请粘贴 AdministratorController 的 preproduct_delete

<pre>undefined method `find' for Administrator:Class</pre>

Administrator 是否继承自 ActiveRecord::Base ?如果没有找到是因为没有。
它应该是

class Administrator < ActiveRecord::Base

authenticate_user!方法:

# See ControllerAdditions#authorize! for documentation. 
def authorize!(action, subject, *args) 
 ....
end   

这不是authenticate_user!方法,这就是授权! 非常不同,你有 before_filterauthenticate_user!在此发布此方法。有东西正在调用 preproduct_delete。

<h1>
  NoMethodError
    in AdministratorController#preproduct_delete
</h1>

Please paste preproduct_delete of AdministratorController

<pre>undefined method `find' for Administrator:Class</pre>

Is Administrator inheriting from ActiveRecord::Base ? if it doesn't have find is because is not.
it should be

class Administrator < ActiveRecord::Base

The authenticate_user! method:

# See ControllerAdditions#authorize! for documentation. 
def authorize!(action, subject, *args) 
 ....
end   

THIS IS NOT the authenticate_user! method, this is authorize! very different, you have before_filter authenticate_user! post this method here. Something is calling preproduct_delete.

爱你不解释 2024-12-28 15:05:41

一种可能的原因是需要显式设置类名。在我的示例中,我将注释资源嵌套在线程资源下,并且我在控制器中编写了以下内容:

class Buy::CommentsController < ApplicationController
  load_and_authorize_resource :thread

这给了我未定义的查找方法错误,因为注释模型位于线程命名空间下。当我像下面这样明确设置它时,它就起作用了。

class Buy::CommentsController < ApplicationController
  load_and_authorize_resource :thread, class: Buy::Thread

One possible reason is that the class name needs to be explicitly set. In my example I had the comments resource nested under the thread resource, and I was writing the following in controller:

class Buy::CommentsController < ApplicationController
  load_and_authorize_resource :thread

This gives me undefined find method error, because comment model was under the thread namespace. When I explicitly set it like the following it works.

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