rails-controller-patterns

ag0os's avatarfrom ag0os

Rails controller patterns and conventions. Automatically invoked when working with controllers, routes, strong parameters, before_actions, or request handling. Triggers on "controller", "action", "routes", "routing", "strong params", "params.expect", "before_action", "respond_to", "RESTful", "CRUD", "redirect", "render".

⭐2stars🔀0forks📁View on GitHub🕐Updated Jan 8, 2026

When & Why to Use This Skill

This Claude skill provides comprehensive patterns and conventions for Ruby on Rails controllers, focusing on RESTful design and secure data handling. It helps developers maintain 'Thin Controllers' by automating the setup of CRUD actions, before_action filters, and Rails 8+ strong parameters (params.expect), ensuring codebases remain clean, scalable, and secure.

Use Cases

  • Standardizing RESTful APIs: Quickly implement index, show, create, and update actions following strict Rails conventions and standard CRUD patterns.
  • Modernizing Parameter Security: Transition to Rails 8+ syntax using params.expect for robust, readable, and secure strong parameter definitions.
  • Optimizing Controller Logic: Refactor complex or bloated controllers to follow the 'Thin Controller' principle, delegating business logic to models or services.
  • Configuring Request Lifecycle: Efficiently set up before_action filters for authentication, resource scoping, and standardized error handling across the application.
namerails-controller-patterns
descriptionRails controller patterns and conventions. Automatically invoked when working with controllers, routes, strong parameters, before_actions, or request handling. Triggers on "controller", "action", "routes", "routing", "strong params", "params.expect", "before_action", "respond_to", "RESTful", "CRUD", "redirect", "render".
allowed-toolsRead, Grep, Glob

Rails Controller Patterns

Patterns for building well-structured Rails controllers.

When This Skill Applies

  • Implementing RESTful controller actions
  • Handling request parameters safely
  • Setting up before_action filters
  • Managing response formats
  • Designing routes
  • Error handling in controllers

Core Principles

Thin Controllers

Controllers should:

  • Handle HTTP concerns (params, session, response)
  • Delegate business logic to models/services
  • Keep actions under 10 lines

RESTful Design

Stick to standard CRUD actions:

  • index, show, new, create, edit, update, destroy
  • Add custom actions sparingly

Detailed Documentation

Controller Structure

class PostsController < ApplicationController
  # 1. Before actions
  before_action :authenticate_user!
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # 2. Actions (CRUD order)
  def index
    @posts = Post.recent.page(params[:page])
  end

  def show
  end

  def new
    @post = current_user.posts.build
  end

  def create
    @post = current_user.posts.build(post_params)

    if @post.save
      redirect_to @post, notice: 'Post created.'
    else
      render :new, status: :unprocessable_entity
    end
  end

  def edit
  end

  def update
    if @post.update(post_params)
      redirect_to @post, notice: 'Post updated.'
    else
      render :edit, status: :unprocessable_entity
    end
  end

  def destroy
    @post.destroy
    redirect_to posts_path, notice: 'Post deleted.'
  end

  private

  # 3. Private methods
  def set_post
    @post = current_user.posts.find(params[:id])
  end

  def post_params
    params.expect(post: [:title, :content, :published])
  end
end

Strong Parameters (Rails 8+)

# Rails 8+ syntax with expect
def post_params
  params.expect(post: [:title, :content, tags: []])
end

# Nested attributes
def user_params
  params.expect(user: [:name, :email, profile_attributes: [:bio, :avatar]])
end

# Traditional permit (still works)
def post_params
  params.require(:post).permit(:title, :content, tags: [])
end

Quick Reference

Action HTTP Verb Path Purpose
index GET /posts List all
show GET /posts/:id Show one
new GET /posts/new Form for new
create POST /posts Create
edit GET /posts/:id/edit Form for edit
update PATCH/PUT /posts/:id Update
destroy DELETE /posts/:id Delete

Key Conventions

  • Use redirect_to after successful changes
  • Use render :action, status: :unprocessable_entity for validation failures
  • Return appropriate HTTP status codes
  • Keep private methods minimal