gpt4 book ai didi

ruby-on-rails - 如何从用户 Controller 中删除设计用户

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:01 25 4
gpt4 key购买 nike

有没有办法从用户 Controller 中销毁用户?

我确实在 routes.rb 中定义了一个 users 资源 并且还在 Users Controller 的方法下方

  def destroy
@user.destroy
head :no_content
end

然而,这会产生以下错误:

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

我正在尝试找出此错误的含义以及相应的修复方法。可能是什么原因?

用户模型

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

堆栈跟踪:

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>'

最佳答案

我已经对此进行了调查,因为我正在尝试 rolifyresourcify User 以外的型号或 Role .确保您拥有最新版本的 rolify(“bundle update rolify”)并重新启动您的服务器和控制台。

handle_dependency错误表明 :roles关联正在被覆盖。

查看源码中的rolify.rb后发现:

<强>1。问题:rolifyresourcify都在创建一个关联 :roles

以下 GitHub 问题解决了尝试 rolify 的问题和 resourcify User .

Undefined method find or create by

要解决此问题,需要进行以下更改:

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

我已经成功解决了 handle_dependency错误,但我关心下一个问题。

<强>2。问题:rolifyresourcify都设置了 adapter

Rolifyresourcify两者都设置相同的实例变量 adapter这是特定于角色或资源的。有人评论说这是个问题here .出于某种原因,我还没有遇到过这种情况,但我预计会遇到。

我的解决方案是替换 adapter在 'rolify.rb' 中有两个新的实例变量 role_adapterresource_adapter并更新 adapter在其余的 rolify 代码中使用特定的所需适配器。这有望让我们能够 rolifyresourcify一个模型。

我还没有测试过这个,但我会在有时间的时候尝试。

关于ruby-on-rails - 如何从用户 Controller 中删除设计用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21279102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com