未定义的方法“find”对于管理员:类 --- CanCan

发布于 2024-10-31 00:18:18 字数 2219 浏览 0 评论 0原文

有没有人看到过这个错误:

undefined method `find' for Admin:Class

When using Devise and CanCan?

我无法找出错误所在:日志没有显示任何有用的内容:

Started GET "/admin/become?id=3" for 127.0.0.1 at Wed Apr 06 13:09:15 -0700 2011
  SQL (0.3ms)  SHOW client_min_messages
  SQL (0.2ms)  SET client_min_messages TO 'panic'
  SQL (0.5ms)  SET standard_conforming_strings = on
  SQL (0.2ms)  SET client_min_messages TO 'notice'
  SQL (0.4ms)  SET time zone 'UTC'
  SQL (0.1ms)  SHOW TIME ZONE
  Processing by AdminController#become as HTML
  Parameters: {"id"=>"3"}
  User Load (2.2ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 2) LIMIT 1
Completed   in 57ms

NoMethodError (undefined method `find' for Admin:Class):


Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.9ms)
  Permission Load (2.5ms)  SELECT * FROM "permissions" INNER JOIN "groups" ON "groups"."id" = "permissions"."group_id" WHERE ("permissions"."user_id" = 2) ORDER BY groups.name ASC
  SQL (1.3ms)   SELECT COUNT(*)
 FROM pg_tables
 WHERE tablename = 'groups'

应用程序跟踪:

cancan (1.5.1) lib/cancan/controller_resource.rb:102:in `find_resource'
cancan (1.5.1) lib/cancan/controller_resource.rb:67:in `load_resource_instance'
cancan (1.5.1) lib/cancan/controller_resource.rb:31:in `load_resource'
cancan (1.5.1) lib/cancan/controller_resource.rb:24:in `load_and_authorize_resource'
cancan (1.5.1) lib/cancan/controller_resource.rb:9:in `send'
cancan (1.5.1) lib/cancan/controller_resource.rb:9:in `_callback_before_35483'
activesupport (3.0.3) lib/active_support/callbacks.rb:456:in `_run__22524425__process_action__2106917469__callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `send'
activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `send'
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `run_callbacks'
actionpack (3.0.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'

Has anyone seen this error:

undefined method `find' for Admin:Class

When using Devise and CanCan?

I'm unable to isolate where this is erroring: The logs are showing nothing useful:

Started GET "/admin/become?id=3" for 127.0.0.1 at Wed Apr 06 13:09:15 -0700 2011
  SQL (0.3ms)  SHOW client_min_messages
  SQL (0.2ms)  SET client_min_messages TO 'panic'
  SQL (0.5ms)  SET standard_conforming_strings = on
  SQL (0.2ms)  SET client_min_messages TO 'notice'
  SQL (0.4ms)  SET time zone 'UTC'
  SQL (0.1ms)  SHOW TIME ZONE
  Processing by AdminController#become as HTML
  Parameters: {"id"=>"3"}
  User Load (2.2ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 2) LIMIT 1
Completed   in 57ms

NoMethodError (undefined method `find' for Admin:Class):


Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.9ms)
  Permission Load (2.5ms)  SELECT * FROM "permissions" INNER JOIN "groups" ON "groups"."id" = "permissions"."group_id" WHERE ("permissions"."user_id" = 2) ORDER BY groups.name ASC
  SQL (1.3ms)   SELECT COUNT(*)
 FROM pg_tables
 WHERE tablename = 'groups'

Application Trace:

cancan (1.5.1) lib/cancan/controller_resource.rb:102:in `find_resource'
cancan (1.5.1) lib/cancan/controller_resource.rb:67:in `load_resource_instance'
cancan (1.5.1) lib/cancan/controller_resource.rb:31:in `load_resource'
cancan (1.5.1) lib/cancan/controller_resource.rb:24:in `load_and_authorize_resource'
cancan (1.5.1) lib/cancan/controller_resource.rb:9:in `send'
cancan (1.5.1) lib/cancan/controller_resource.rb:9:in `_callback_before_35483'
activesupport (3.0.3) lib/active_support/callbacks.rb:456:in `_run__22524425__process_action__2106917469__callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `send'
activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `send'
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `run_callbacks'
actionpack (3.0.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'

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

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

发布评论

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

评论(1

ゝ杯具 2024-11-07 00:18:18

背景:请参阅问题中的上述评论,包括 上一个问题,以及此链接了解详细信息。

您的问题源于没有基于设计的管理类。我从 NoMethodFound 错误中收集了此信息。这意味着您无法在其上使用任何类型的 ActiveRecord 方法,而基本实现正在尝试执行此操作。 替换这行代码...

return unless current_user.is_an_admin?

您可能想用这样的东西

return unless current_user.admin == true

(或者可以在您的用户模型表中找到的东西)

Background: See the comments above in the question, including the previous question, and this link for details.

Your problem stems from not having a Devise based Admin class. I gleaned this information from the NoMethodFound error. This means that you are unable to use any kind of ActiveRecord methods on it, which the base implementation is trying to do. You'll probably want to replace this line of code...

return unless current_user.is_an_admin?

with something like this...

return unless current_user.admin == true

(or something that can be found in your User model's table)

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