如何从用户控制器中删除设备用户
有没有办法从用户控制器中销毁用户?
我确实在routes.rb
定义了users resource
,并且还有Users Controller
方法
def destroy @user.destroy head :no_content end
然而,这会产生以下错误:
undefined method `handle_dependency' for #
我正在尝试找出此错误代表的内容以及适当的修复方法。 可能是什么原因?
用户模型
class User :before_add_method # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable has_many :venues, dependent: :destroy has_many :from_user_chats, :foreign_key => 'from_user_id', :class_name => 'Chat' has_many :to_user_chats, :foreign_key => 'to_user_id', :class_name => 'Chat' has_one :uploaded_file, as: :imageable, dependent: :destroy accepts_nested_attributes_for :uploaded_file, :reject_if => proc { |attributes| attributes['assets'].blank? } def name "#{first_name} #{last_name}" end def before_add_method(role) # do something end def assign_default_role add_role :visitor end def self.from_omniauth(auth) where(auth.slice(:provider, :uid)).first_or_create do |user| user.provider = auth.provider user.uid = auth.uid user.username = auth.info.nickname end end def self.new_with_session(params, session) if session["devise.user_attributes"] new(session["devise.user_attributes"], without_protection: true) do |user| user.attributes = params user.valid? end else super end end def password_required? super && provider.blank? end def update_with_password(params, *options) if encrypted_password.blank? update_attributes(params, *options) else super end end end
堆栈跟踪:
Started DELETE "/users/6" for 127.0.0.1 at 2014-01-22 20:33:08 +1100 Processing by UsersController#destroy as HTML Parameters: {"authenticity_token"=>"T5pbLtoI0MAiaFW0x24LXdlospa4b8ogVtdLbhqEYyc=", "id"=>"6"} [1m[36mUser Load (1.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1[0m [1m[35mUploadedFile Load (0.0ms)[0m SELECT "uploaded_files".* FROM "uploaded_files" WHERE "uploaded_files"."imageable_id" = $1 AND "uploaded_files"."imageable_type" = $2 ORDER BY "uploaded_files"."id" ASC LIMIT 1 [["imageable_id", 1], ["imageable_type", "User"]] [1m[36mUser Load (0.0ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1[0m [["id", "6"]] [1m[35m (0.0ms)[0m SELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL))) [["user_id", 1]] [1m[36m (0.0ms)[0m [1mSELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND "roles"."name" = 'admin'[0m [["user_id", 1]] [1m[35mRole Load (0.0ms)[0m SELECT "roles".* FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND "roles"."name" = 'admin' [["user_id", 1]] [1m[36m (1.0ms)[0m [1mSELECT "products"."id" FROM "products" INNER JOIN "roles" ON "roles".resource_type = 'Product' AND ("roles".resource_id IS NULL OR "roles".resource_id = "products".id) WHERE ("roles".name IN ('admin') AND "roles".resource_type = 'Product') AND ("roles".id IN (3) AND ((resource_id = "products".id) OR (resource_id IS NULL)))[0m [1m[35mCACHE (0.0ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", "6"]] [1m[36mUploadedFile Load (0.0ms)[0m [1mSELECT "uploaded_files".* FROM "uploaded_files" WHERE "uploaded_files"."imageable_id" = $1 AND "uploaded_files"."imageable_type" = $2 ORDER BY "uploaded_files"."id" ASC LIMIT 1[0m [["imageable_id", 6], ["imageable_type", "User"]] [1m[35m (0.0ms)[0m BEGIN [1m[36m (0.0ms)[0m [1mROLLBACK[0m Completed 500 Internal Server Error in 20ms NoMethodError - undefined method `handle_dependency' for #: activerecord (4.0.0) lib/active_record/associations/builder/association.rb:97:in `has_many_dependent_for_roles' activesupport (4.0.0) lib/active_support/callbacks.rb:377:in `_run__282840259__destroy__callbacks' activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks' activerecord (4.0.0) lib/active_record/callbacks.rb:289:in `destroy' activerecord (4.0.0) lib/active_record/transactions.rb:265:in `block in destroy' activerecord (4.0.0) lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction' activerecord (4.0.0) lib/active_record/transactions.rb:209:in `transaction' activerecord (4.0.0) lib/active_record/transactions.rb:323:in `with_transaction_returning_status' activerecord (4.0.0) lib/active_record/transactions.rb:265:in `destroy' app/controllers/users_controller.rb:54:in `destroy' actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action' actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action' actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action' actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action' activesupport (4.0.0) lib/active_support/callbacks.rb:463:in `_run__462102624__process_action__callbacks' activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks' actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action' actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action' actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action' activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument' activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument' activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument' actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action' actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action' activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action' actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process' actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process' actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch' actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch' actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call' actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call' actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call' actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call' omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!' omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call' omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!' omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call' omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!' omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call' omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!' omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call' omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!' omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call' omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!' omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call' omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!' omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call' omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!' omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call' omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!' omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call' omniauth (1.1.4) lib/omniauth/builder.rb:49:in `call' bullet (4.7.1) lib/bullet/rack.rb:10:in `call' warden (1.2.3) lib/warden/manager.rb:35:in `block in call' warden (1.2.3) lib/warden/manager.rb:34:in `call' rack (1.5.2) lib/rack/etag.rb:23:in `call' rack (1.5.2) lib/rack/conditionalget.rb:35:in `call' rack (1.5.2) lib/rack/head.rb:11:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call' rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context' rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call' activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call' activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call' activerecord (4.0.0) lib/active_record/migration.rb:369:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__867238995__call__callbacks' activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks' actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call' better_errors (1.0.1) lib/better_errors/middleware.rb:84:in `protected_app_call' better_errors (1.0.1) lib/better_errors/middleware.rb:79:in `better_errors_call' better_errors (1.0.1) lib/better_errors/middleware.rb:56:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app' railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call' activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged' activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged' activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged' railties (4.0.0) lib/rails/rack/logger.rb:21:in `call' quiet_assets (1.0.2) lib/quiet_assets.rb:18:in `call_with_quiet_assets' actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call' rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' rack (1.5.2) lib/rack/runtime.rb:17:in `call' activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call' rack (1.5.2) lib/rack/lock.rb:17:in `call' actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call' railties (4.0.0) lib/rails/engine.rb:511:in `call' railties (4.0.0) lib/rails/application.rb:97:in `call' rack (1.5.2) lib/rack/content_length.rb:14:in `call' thin (1.6.1) lib/thin/connection.rb:82:in `block in pre_process' thin (1.6.1) lib/thin/connection.rb:80:in `pre_process' thin (1.6.1) lib/thin/connection.rb:55:in `process' thin (1.6.1) lib/thin/connection.rb:41:in `receive_data' eventmachine-1.0.3-x86 (mingw32) lib/eventmachine.rb:187:in `run' thin (1.6.1) lib/thin/backends/base.rb:73:in `start' thin (1.6.1) lib/thin/server.rb:162:in `start' rack (1.5.2) lib/rack/handler/thin.rb:16:in `run' rack (1.5.2) lib/rack/server.rb:264:in `start' railties (4.0.0) lib/rails/commands/server.rb:84:in `start' railties (4.0.0) lib/rails/commands.rb:78:in `block in ' railties (4.0.0) lib/rails/commands.rb:73:in `' bin/rails:4:in `'
我已经对此进行了调查,因为我正在尝试对User
或Role
以外的模型进行rolify
和再rolify
。 确保您拥有最新版本的rolify(“捆绑更新rolify”)并重新启动服务器和控制台。
handle_dependency
错误表示正在覆盖:roles
关联。
在源代码中查看rolify.rb后,我发现:
1.问题: rolify
和rolify
都创建了一个关联:roles
以下GitHub问题解决了尝试rolify
和重新rolify
User
。
未定义的方法查找或创建
要解决此问题,需要进行以下更改:
class User < ActiveRecord::Base resourcify :resources # Must be placed before rolify
我已成功解决handle_dependency
错误,但我关注下一个问题。
2.问题: rolify
和rolify
都设置了adapter
Rolify
和Rolify
都设置相同的角色或资源的实例变量adapter
。 有人评论说这是一个问题。 出于某种原因,我还没有遇到过这个问题,但我希望如此。
我的解决方案是使用两个新的实例变量role_adapter
和resource_adapter
替换'rolify.rb'中的adapter
,并使用特定的所需适配器在整个rolify代码中更新适配器。 这有望实现模型的rolify
和再rolify
。
我还没有测试过这个,但是当我有时间的时候我会试试。
因为看起来你正在使用自定义Devise控制器,所以你可以尝试在你的destroy动作中加入super
。 这意味着它将inheritance原始注册控制器。 这可能是这样的:
def destroy super end
希望这可以帮助!