askill
vanilla-rails-routing

vanilla-rails-routingSafety 100Repository

Use when defining Rails routes, organizing controller directories, or adding resources - enforces resource extraction, shallow nesting, resolve helpers, and module scoping

2 stars
1.2k downloads
Updated 2/14/2026

Package Files

Loading files...
SKILL.md

Vanilla Rails Routing

Routes mirror controller directory structure. Every route points to a controller in the matching namespace.

Resource Extraction (State as Resources)

State changes become singular nested resources, not custom actions:

# Bad - custom actions
resources :cards do
  post :close
  post :archive
end

# Good - state as resource
resources :cards do
  resource :closure, only: [:create, :destroy]
  resource :archival, only: [:create, :destroy]
end

resource (singular) = one state per parent. create = enter state. destroy = leave state.

Controller Directory Mapping

Route nesting mirrors app/controllers/ directory:

# routes.rb
resources :boards do
  resources :cards do
    resource :closure
  end
end

# Maps to:
# app/controllers/boards_controller.rb
# app/controllers/boards/cards_controller.rb
# app/controllers/boards/cards/closures_controller.rb

Rule: If a route is nested under boards, the controller lives in boards/ directory.

Shallow Nesting

Avoid deeply nested URLs. Use shallow: or flatten when resource has its own identity:

# Deep (avoid for >2 levels)
/boards/1/cards/2/comments/3/reactions/4

# Shallow
resources :boards do
  resources :cards, shallow: true
end
# Produces:
# /boards/:board_id/cards     (index, new, create)
# /cards/:id                  (show, edit, update, destroy)

Rule: If you can look up the resource by its own ID, it doesn't need parent in URL.

Module Scoping

Group related controllers under modules for admin/API/etc:

namespace :admin do
  resources :accounts    # Admin::AccountsController
end

scope module: :api do
  resources :cards       # Api::CardsController (no /api prefix in URL)
end

scope "/api" do
  resources :cards       # CardsController (with /api prefix, no module)
end
MethodURL prefixModule prefixUse when
namespaceYesYesAdmin panel, API versioning
scope module:NoYesAlternate controller, same URL
scope path:YesNoURL prefix, same controller

Resolve Helpers

Use resolve to control polymorphic URL generation:

# Without resolve: url_for(@card) might generate wrong path
resolve("Card") { |card| [card.board, card] }

# Now polymorphic_path(@card) generates /boards/:board_id/cards/:id

Use when polymorphic routing needs explicit parent context.

Direct Routes

Name custom routes for URL helpers:

direct(:homepage) { "/" }
direct(:dashboard) { |account| "/#{account.to_param}/dashboard" }

# Usage: homepage_url, dashboard_url(@account)

Quick Reference

PatternUseAvoid
resource :closureSingular state resourcepost :close
resources :cards, shallow: true2+ nesting levelsDeep nesting >3 levels
namespace :adminAdmin/API sectionsManual path + module
resolve("Card")Polymorphic URL controlString interpolation in paths
Controller at boards/cards_controller.rbNested route under boardsController location mismatch

Red Flags

  • Custom member/collection actions for state changes → extract resource
  • Controller file not matching route nesting → move controller
  • 3+ levels of URL nesting → use shallow: or flatten
  • url_for generating wrong paths → add resolve
  • Route with no corresponding controller directory → create directory

Install

Download ZIP
Requires askill CLI v1.0+

AI Quality Score

88/100Analyzed 2/19/2026

Well-structured Rails routing reference with comprehensive coverage of resource extraction, shallow nesting, module scoping, and resolve helpers. Includes actionable code examples, tables, and clear "when to use" guidance. Slightly path suggests organization-specific but content is general and reusable.

100
85
85
90
90

Metadata

Licenseunknown
Version-
Updated2/14/2026
PublisherZempTime

Tags

api