查看源代码 路由速查表

这些需要在正确的路由模块和作用域中声明。

常见路由功能语法的快速参考。有关详尽概述,请参阅路由指南

路由声明

单一路由

get "/users", UserController, :index
patch "/users/:id", UserController, :update
# generated routes
~p"/users"
~p"/users/9" # user_id is 9

也接受 putpatchoptionsdeletehead

资源

简单

resources "/users", UserController

生成 :index:edit:new:show:create:update:delete

选项

resources "/users", UserController, only: [:show]
resources "/users", UserController, except: [:create, :delete]
resources "/users", UserController, as: :person # ~p"/person"

嵌套

resources "/users", UserController do
  resources "/posts", PostController
end
# generated routes
~p"/users/3/posts" # user_id is 3
~p"/users/3/posts/17" # user_id is 3 and post_id = 17

有关更多信息,请查看资源文档

作用域

简单

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

  resources "/users",   UserController
end
# generated path helpers
~p"/admin/users"

嵌套

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

  scope "/v1", V1, as: :v1 do
    resources "/users", UserController
  end
end
# generated path helpers
~p"/api/v1/users"

有关更多信息,请查看作用域路由 文档。