简体   繁体   中英

How to delete devise user from users controller

Is there a way to destroy user from a users controller?

I do have a users resource defined in routes.rb and also below method for Users Controller

  def destroy
    @user.destroy
    head :no_content
  end

This however creates below error:

undefined method `handle_dependency' for #<ActiveRecord::Associations::HasAndBelongsToManyAssociation:0x8839440>

I'm trying to locate what this error stands for and the appropriate fix for it. What could be the cause?

User model

class User < ActiveRecord::Base
  resourcify

  after_create :assign_default_role
  rolify :before_add => :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

Stack trace:

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::Associations::HasAndBelongsToManyAssociation:0x8839440>:
  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 <top (required)>'
  railties (4.0.0) lib/rails/commands.rb:73:in `<top (required)>'
  bin/rails:4:in `<main>'

I've investigated this as I'm trying to rolify and resourcify a model other than User or Role . Make sure you have the latest version of rolify ("bundle update rolify") and restart your server and console.

The handle_dependency error indicates that the :roles association is being overwritten.

After looking at rolify.rb in the source, I found:

1. Problem: rolify and resourcify are both creating an association :roles

The following GitHub issue addresses attempting to rolify and resourcify User .

Undefined method find or create by

To resolve this issue, the following change is needed:

class User < ActiveRecord::Base
  resourcify :resources # Must be placed before rolify

I've had success resolving the handle_dependency error, but I'm concerned with the next problem.

2. Problem: rolify and resourcify are both setting an adapter

Rolify and resourcify both set the same instance variable adapter that is role or resource specific. Someone has commented that this is a problem here . For some reason I haven't encountered this yet, but I expect to.

My solution would be to replace adapter in 'rolify.rb' with two new instance variables role_adapter and resource_adapter and update adapter throughout the rest of the rolify code with the specific required adapter. This hopefully will allow the ability to rolify and resourcify a model.

I haven't tested this yet but I'll try when I have time.

Since it looks like you're using a custom Devise controller, you can try putting super in your destroy action. This means that it will inherit from the original registrations controller. This is what it might look like:

def destroy
 super
end

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM