ruby
41 lines · 8 steps
A guarded destroy action in Rails
A namespaced admin controller deletes an account only after confirmation, protection, and transactional safety checks.
Explained by
highlit
1class Admin::AccountsController < Admin::BaseController
2 before_action :set_account, only: :destroy
3
4 def destroy
5 if params[:confirm_name] != @account.name
6 redirect_to admin_account_path(@account),
7 alert: "Type the account name exactly to confirm deletion."
8 return
9 end
10
11 if @account.protected?
12 redirect_to admin_account_path(@account),
13 alert: "This account is protected and cannot be deleted."
14 return
15 end
16
17 @account.transaction do
18 @account.memberships.destroy_all
19 @account.destroy!
20 end
21
22 AccountAuditLog.record!(
23 action: :destroyed,
24 target: @account,
25 actor: current_admin,
26 metadata: { name: @account.name }
27 )
28
29 redirect_to admin_accounts_path,
30 notice: "Account #{@account.name} was permanently deleted."
31 rescue ActiveRecord::RecordNotDestroyed => e
32 redirect_to admin_account_path(@account),
33 alert: "Could not delete account: #{e.message}"
34 end
35
36 private
37
38 def set_account
39 @account = Account.find(params[:id])
40 end
41end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Guard clauses with early returns keep destructive actions readable by rejecting bad requests before any mutation happens.
- 2Wrapping dependent deletions in a transaction ensures the record and its associations are removed atomically or not at all.
- 3Recording an audit log after a successful destroy preserves accountability for irreversible operations.
Related explainers
ruby
class MailingListDeduplicator GMAIL_DOMAINS = %w[gmail.com googlemail.com].freeze def initialize(subscribers)
Deduplicating a mailing list by canonical email
deduplication
normalization
service object
Intermediate
8 steps
ruby
class ProgressBar BAR_WIDTH = 40 def initialize(total, io: $stdout)
Building a terminal progress bar in Ruby
state tracking
terminal output
time estimation
Intermediate
8 steps
php
<?php namespace App\Services;
Idempotent role assignment in Laravel
many-to-many
pivot-data
idempotency
Intermediate
8 steps
ruby
class Product < ApplicationRecord scope :search, ->(query) { return all if query.blank?
Building a multi-term search scope in Rails
arel
scopes
full-text search
Advanced
7 steps
ruby
module Rack class MaintenanceMode RETRY_AFTER = 3600
A Rack maintenance-mode middleware in Rails
middleware
rack
http-status
Intermediate
8 steps
php
<?php namespace App\Services;
How user impersonation works in Laravel
authentication
authorization
session
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-guarded-destroy-action-in-rails-explained-ruby-2dd4/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.