guest@websec: ~/websec/r09-rest-routes — zsh — 100×40 by SaKKo YouTube ENTH

guest@websec ~ % man r09-rest-routes

RESTful and Resourceful Routes

REST means: URLs name things (resources), HTTP verbs say what to do with them. Rails' resources turns that idea into routes, controller actions and helper methods in one line.

Name URLs after plural resources (/client_applications/:id/roles), let HTTP verbs express the action, restrict unused actions with only:, use POST member routes for state transitions, and reference paths via named helpers (…_path / …_url).

Five actions cover almost everything

verbpathactionmeaningsuccess
GET/client_applications#indexlist200
POST/client_applications#createcreate one201
GET/client_applications/:id#showread one200
PATCH / PUT/client_applications/:id#updatechange one200
DELETE/client_applications/:id#destroyremove one (for us: soft delete)200
API-only apps skip new and edit: those actions only exist to render HTML forms.

Good paths, bad paths

✗

POST /api/getRoles?appId=42
POST /api/v1/admin/createRole
GET  /api/v1/admin/deleteUser?id=7
POST /api/v1/admin/role/update
GET  /api/v1/admin/RolesForApp/42

✓

GET    /api/v1/admin/client_applications/:id/roles
POST   /api/v1/admin/client_applications/:id/roles
DELETE /api/v1/admin/users/:id
PATCH  /api/v1/admin/client_applications/:ca_id/roles/:id
POST   /api/v1/admin/users/:id/toggle_admin
  • Plural nouns, snake_case, versioned under /api/v1.
  • The verb is the HTTP method. Never change data with GET: browsers, proxies and crawlers repeat GETs freely.
  • Ownership lives in the path: roles of an application are nested under it.
  • A verb in the path is allowed only as a member action for a state change that isn’t plain CRUD (toggle_admin, activate).

Resourceful routing

resources :client_applications, only: %i[ index show create update ]   # always list actions

resources :client_applications do
  resources :roles, only: %i[ index show create update ]                # nested: /client_applications/:client_application_id/roles
end

resources :users, only: %i[ index show ] do
  member do
    post :toggle_admin                       # /users/:id/toggle_admin      → acts on one
  end
  collection do
    get :export                              # /users/export                → acts on the set
  end
end

resource :profile, only: %i[ show update ]   # singular: no :id, it's "mine" → profiles#show

namespace :api do                            # path /api/..., module Api::, folder api/, helper api_...
  namespace :v1 do
    namespace :admin do
      resources :users
    end
  end
end
Always pass only:. Routes you don't use are routes someone can probe, and they also become permissions in Permission.all_admin_permissions. Nest one level at most.

State changes as member routes

Each AASM event on a model becomes a POST member route and a controller action that calls the bang event. The admin UI reads actionables from the JSON to know which buttons to show:

# POST /api/v1/admin/users/:user_id/user_roles/:id/activate
def activate
  @user_role.activate!                                   # AASM::InvalidTransition → 400
  render json: { user_role: @user_role.as_admin_json }   # includes actionables: ["inactivate"]
end

That beats PATCH status=active: the transition rules stay in the model, each event gets its own permission, and audit logs say what happened.

Non-resource routes

namespace :admin do
  post   "sessions",    to: "sessions#create"    # login       → api_v1_admin_sessions
  get    "sessions/me", to: "sessions#me"        # who am I    → api_v1_admin_sessions_me
  delete "sessions",    to: "sessions#destroy"   # logout
end

Some endpoints are actions rather than things (login, verify a token). Write them with verb "path", to: "controller#action". Rails derives the helper name from the path.

Build routes, see the result

route buildertoggle options, then click a row

Reading rails routes

Named route helpers

Every Prefix becomes two methods: …_path (just the path) and …_url (with host). Try them in the console through app:

All our routes

Rails.application.routes.draw do
  get "health", to: "application#health"

  namespace :api do
    namespace :v1 do
      namespace :admin do
        post   "sessions",    to: "sessions#create"
        get    "sessions/me", to: "sessions#me"
        delete "sessions",    to: "sessions#destroy"

        resources :users, only: %i[ index show ] do
          member do
            post :toggle_admin
          end
          resources :user_roles, only: %i[ index create ] do
            member do
              post :activate
              post :inactivate
            end
          end
        end

        resources :client_applications, only: %i[ index show create update ] do
          resources :roles, only: %i[ index show create update ]
          resources :permissions, only: %i[ index ]
        end
      end

      namespace :client do
        post "auth/login",  to: "auth#login"
        get  "auth/verify", to: "auth#verify"
        resources :roles, only: %i[ index ]
        resources :permissions, only: %i[ index ]
      end

      namespace :public do
        post "two_factors/enroll",  to: "two_factors#enroll"
        post "two_factors/confirm", to: "two_factors#confirm"
        post "two_factors/disable", to: "two_factors#disable"
      end
    end
  end
end

Check yourself

Admins need to disable a client application without deleting it. Which route fits our style?

A state change that isn't CRUD: an AASM event exposed as a POST member route.

What is the helper for GET /api/v1/admin/users/:user_id/user_roles?

Namespace + parent (singular) + child (plural): api_v1_admin + user + user_roles.