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 开发者_StackOverflow社区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'
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)
精彩评论