Skip to content

Commit a119b66

Browse files
committed
Fix user deletion foreign key constraint issue and add error handling
1 parent 29a45bb commit a119b66

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

app/controllers/users/registrations_controller.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,26 @@ def confirm_delete
77

88
# Ensure we redirect to a public page with a clear flash after account deletion
99
def destroy
10-
resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
10+
resource = resource_class.to_adapter.get!(send("current_#{resource_name}").to_key)
1111

12-
# Destroy user account
13-
resource.destroy
12+
begin
13+
# Destroy user account
14+
if resource.destroy
15+
# Sign out and set the Devise flash message
16+
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
17+
redirect_to(home_path, status: :see_other, notice: find_message(:destroyed))
18+
else
19+
# If resource was not destroyed due to validation errors
20+
redirect_to(edit_user_registration_path, alert: resource.errors.full_messages.to_sentence)
21+
end
22+
rescue => e
23+
# Log the error for debugging
24+
Rails.logger.error "Failed to delete user account: #{e.message}"
25+
Rails.logger.error e.backtrace.join("\n")
1426

15-
# Sign out and set the Devise flash message
16-
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
17-
redirect_to(home_path, status: :see_other, notice: find_message(:destroyed))
27+
# Redirect with helpful error message
28+
redirect_to(edit_user_registration_path,
29+
alert: "Unable to delete account due to a system error. Please try again later or contact support.")
30+
end
1831
end
1932
end

app/models/user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ class User < ApplicationRecord
55
:recoverable, :rememberable, :validatable
66

77
has_many :entries, dependent: :destroy
8+
has_many :audit_events, dependent: :destroy
89
end

0 commit comments

Comments
 (0)