Admin的未定义方法`find':Class --- CanCan

时间:2011-04-06 20:16:46

标签: ruby-on-rails ruby-on-rails-3 devise cancan

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

undefined method `find' for Admin:Class

使用Devise和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'

1 个答案:

答案 0 :(得分:4)

背景:有关详细信息,请参阅问题中的上述评论,包括previous questionthis link

您的问题源于没有基于Devise的Admin类。我从NoMethodFound错误中收集了这些信息。这意味着您无法在其上使用任何类型的ActiveRecord方法,基本实现正在尝试这样做。你可能想要替换这行代码......

return unless current_user.is_an_admin?

有这样的......

return unless current_user.admin == true

(或可在用户模型表中找到的内容)