Routing cheatsheet

View Source

Those need to be declared in the correct router module and scope.

A quick reference to the common routing features' syntax. For an exhaustive overview, refer to the routing guides.

Routing declaration

Single route

get "/users", UserController, :index
patch "/users/:id", UserController, :update
# Verified Routes
~p"/users"
~p"/users/#{@user}"

Also accepts put, patch, options, delete and head.

Resources

Simple

resources "/users", UserController

Generates :index, :edit, :new, :show, :create, :update and :delete.

Options

resources "/users", UserController, only: [:show]
resources "/users", UserController, except: [:create, :delete]

Nested

resources "/users", UserController do
  resources "/posts", PostController
end
# Verified Routes
~p"/users"
~p"/users/new"
~p"/users/#{@user}"
~p"/users/#{@user}/edit"
~p"/users/#{@user}/posts"
~p"/users/#{@user}/posts/new"
~p"/users/#{@user}/posts/#{@post}"
~p"/users/#{@user}/posts/#{@post}/edit"

For more info check the resources docs.

Scopes

Simple

scope "/admin", HelloWeb.Admin do
  pipe_through :browser

  resources "/users", UserController
end
# Verified Routes
~p"/admin/users"
~p"/admin/users/new"
~p"/admin/users/#{@user}"
~p"/admin/users/#{@user}/edit"

Nested

scope "/api", HelloWeb.Api do
  pipe_through :api

  scope "/v1", V1 do
    resources "/users", UserController
  end
end
# Verified Routes
~p"/api/v1/users"
~p"/api/v1/users/new"
~p"/api/v1/users/#{@user}"
~p"/api/v1/users/#{@user}/edit"

For more info check the scoped routes docs.