Skip to content

Routing

Routing is the only file that describes an application’s entire public surface. Reading config/routes.rb should tell you what the app does and what it accepts. Routes that are hard to read are usually routes that are hard to secure.

“resources :posts generates seven routes mapping verb and path to a controller action. Each gets a named helper — posts_path, new_post_path, post_path(post), edit_post_path(post). Nesting expresses ownership: resources :posts do resources :comments end gives /posts/:post_id/comments, and shallow: true drops the parent segment for member routes. Namespaces add a path prefix, a module and a helper prefix at once; scope adds only what you ask for. Constraints reject requests before a controller is instantiated. Narrow every resource with only:/except: — a route you do not need is an endpoint someone can POST to.”

Helper Verb Path Action
posts_path GET /posts index
new_post_path GET /posts/new new
posts_path POST /posts create
post_path(post) GET /posts/:id show
edit_post_path(post) GET /posts/:id/edit edit
post_path(post) PATCH/PUT /posts/:id update
post_path(post) DELETE /posts/:id destroy

Prefer a new resource over a custom action: POST /posts/:id/publication handled by PublicationsController#create is easier to authorise than posts#publish.

resources :posts do
resources :comments, shallow: true
end
GET /posts/:post_id/comments comments#index
POST /posts/:post_id/comments comments#create
GET /comments/:id comments#show
PATCH /comments/:id comments#update
DELETE /comments/:id comments#destroy

Nest one level only. Three ids to identify a row that already has its own id is a sign to shallow or to scope by the immediate parent. shallow: true also works as a shallow do ... end block around several resources.

namespace :admin do
resources :users # /admin/users → Admin::UsersController, admin_users_path
end
scope module: "admin" do
resources :users # /users → Admin::UsersController
end
scope "/admin" do
resources :users # /admin/users → UsersController
end

namespace sets path prefix, module and helper prefix together. scope sets only what you pass — which is how you get an admin path without an admin controller module, or vice versa.

namespace :api do
namespace :v1 do
resources :posts, only: [:index, :show]
end
end

Or version via headers and keep one controller, with a constraint.

get "/posts/:id", to: "posts#show", constraints: { id: /\d+/ }
class ApiConstraint
def self.matches?(request)
request.headers["Accept"]&.include?("application/json")
end
end
constraints ApiConstraint do
resources :posts, only: [:index]
end
constraints subdomain: "api" do
resources :posts, only: [:index]
end

Constraints run before the controller, so a rejected request never allocates one. Also available: defaults: { format: :json }, constraints(host: ...), and request.format.

post_path(post) # "/posts/42"
post_url(post) # "https://example.com/posts/42" — use in emails
posts_path(page: 2) # "/posts?page=2"
url_for(post) # post_url for a persisted record, new_post_path for Post.new
redirect_to [post, comment] # post_comment_path(post, comment)

Use helpers, never string paths: a hardcoded "/posts/#{post.id}" survives a route change as a bug. url_for and redirect_to accept anything implementing to_model, which is how decorators stay route-agnostic.

  1. What is the difference between namespace, scope module: and scope path:?
  2. Why is shallow: true usually better than deep nesting?
  3. How do member and collection routes differ in the helper they generate?
  4. What does a routing constraint run before, and why is that a performance win?
  5. How would you handle a PATCH that some clients send as PUT?
  • Catch-all routes. get "*path", to: "pages#show" swallows 404s and makes missing pages look like server errors.
  • match without via:. Accepts every verb, including ones you never intended.
  • Deep nesting. Three levels means a helper with three arguments, which nobody remembers.
  • Forgetting only:. Every unused route is an endpoint someone can POST to.
  • Defining root twice. The last one wins, silently.
  • Hardcoded paths in views. They survive refactors as bugs.