skills/rails-controller-patterns/SKILL.md
Analyzes and recommends Rails controller patterns including RESTful design, strong parameters, before_actions, response handling, and routing. Use when building controllers, defining routes, handling params, or managing request/response flow. NOT for model validations, service object internals, view templates, or background job logic.
npx skillsauth add ag0os/rails-dev-plugin rails-controller-patternsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Generate controllers following standard Rails RESTful conventions (7 actions, resourceful routing, before_actions, strong params).
rails-stack-profiles), native delegates to models and concerns, extracted delegates to service objectsparams.expect (Rails 8+)redirect_to after success, render :action, status: on failureCards::ClosuresController over post :close on CardsControllerparams.expect replaces params.require().permit() with a cleaner, more secure API. Check Gemfile.lock for Rails version before using.
# Basic
params.expect(post: [:title, :content])
# Arrays
params.expect(post: [:title, tags: []])
# Nested attributes
params.expect(user: [:name, :email, profile_attributes: [:bio, :avatar]])
# Dynamic hash attributes
params.expect(product: [:name, :price, metadata: {}])
# Legacy syntax (Rails < 8)
params.require(:post).permit(:title, :content)
Treat state changes as resources instead of adding custom member actions. This keeps controllers focused and RESTful.
# Bad - custom actions bloat the controller
resources :cards do
post :close
post :reopen
end
# Good - resource controllers
resources :cards do
scope module: :cards do
resource :closure # create = close, destroy = reopen
resource :pin # create = pin, destroy = unpin
end
end
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from ActionPolicy::Unauthorized, with: :forbidden
private
def not_found
respond_to do |format|
format.html { render "errors/not_found", status: :not_found }
format.json { render json: { error: "Not found" }, status: :not_found }
end
end
def forbidden
respond_to do |format|
format.html { redirect_back fallback_location: root_path, alert: "Not authorized." }
format.json { render json: { error: "Forbidden" }, status: :forbidden }
end
end
end
| Anti-Pattern | Problem | Fix |
|-------------|---------|-----|
| Fat controllers (50+ line actions) | Hard to test and maintain | Move logic to its axis-appropriate home (see Principle 1) |
| Business logic in controllers | Violates SRP | Move logic to its axis-appropriate home (see Principle 1) |
| Custom member actions (:close, :archive) | Controller grows unbounded | Create dedicated resource controllers (Cards::ClosuresController) |
| params.permit! | Allows all params (mass assignment) | Use params.expect with explicit fields |
| Deeply nested routes (>1 level) | Confusing URLs and helpers | Use shallow: true or flat routes |
| Skipping authentication filters | Security vulnerability | Apply before_action broadly, skip selectively |
| Multiple respond_to blocks | Code duplication | Use respond_to once or separate API controllers |
status: :unprocessable_entity (422) for validation failuresstatus: :not_found (404) for missing recordsstatus: :forbidden (403) for authorization failuresstatus: :see_other (303) for redirect_to after DELETErescue_from in ApplicationController for consistent error responsesWhen analyzing or creating controllers, provide:
config/routes.rbdevelopment
WHAT: Language-agnostic corrective guidance for the refactoring phase. WHEN: Agent is restructuring code, fixing code smells, reducing complexity, or improving maintainability. NOT FOR: Writing new features, debugging runtime errors, performance tuning, or object design decisions.
tools
Analyzes Rails view templates, partials, layouts, helpers, and form patterns for best practices. Use when reviewing ERB templates, improving view performance with fragment caching, fixing form helpers, organizing partials, adding accessibility attributes, or evaluating collection rendering. NOT for Stimulus/Turbo logic (use hotwire-patterns), controller concerns, or API-only responses.
testing
Analyzes Rails test suites and recommends testing best practices for RSpec and Minitest. Use when writing new tests, reviewing test coverage, fixing flaky tests, improving test performance, choosing between test types (unit, integration, system, request), or setting up factories and fixtures. NOT for production monitoring, deployment verification, or load/stress testing infrastructure.
development
Detects a Rails project's architecture axes — logic placement (native vs extracted) and delivery (html vs api) — so other skills load profile-appropriate guidance without inline conditionals. Use when planning architecture or when a recommendation depends on where business logic lives or whether the app renders HTML or serves JSON. NOT for test framework, job backend, cache store, or auth library choices — those are orthogonal facts detected by project-conventions.