如何使用 Phoenix 框架 Router 中的资源更改参数名称
How to change the parameter name using resources in Phoenix framework Router
是否可以选择制作这个 ->
resources "users", MyApp.UserController
生成参数名称不是 :id
的路径?
在resources/4 macro中使用param
参数:
resources "users", MyApp.UserController, param: "name"
这将生成以下路由:
- 获取/用户 => :index
- GET /users/new => :new
- POST /用户 => :创建
- GET /users/:name => :show
- GET /users/:name/edit => :edit
- PATCH /users/:name => :update
- PUT /users/:name => :update
- DELETE /users/:name => :delete
以下是资源的附加选项:
This macro accepts a set of options:
:only - a list of actions to generate routes for, for example: [:show, :edit]
:except - a list of actions to exclude generated routes from, for example: [:delete]
:param - the name of the paramter for this resource, defaults to "id"
:name - the prefix for this resource. This is used for the named helper and as the prefix for the parameter in nested resources. The default value is automatically derived from the controller name, i.e. UserController will have name "user"
:as - configures the named helper exclusively
:singleton - defines routes for a singleton resource that is looked up by the client without referencing an ID. Read below for more information
是否可以选择制作这个 ->
resources "users", MyApp.UserController
生成参数名称不是 :id
的路径?
在resources/4 macro中使用param
参数:
resources "users", MyApp.UserController, param: "name"
这将生成以下路由:
- 获取/用户 => :index
- GET /users/new => :new
- POST /用户 => :创建
- GET /users/:name => :show
- GET /users/:name/edit => :edit
- PATCH /users/:name => :update
- PUT /users/:name => :update
- DELETE /users/:name => :delete
以下是资源的附加选项:
This macro accepts a set of options:
:only - a list of actions to generate routes for, for example: [:show, :edit]
:except - a list of actions to exclude generated routes from, for example: [:delete]
:param - the name of the paramter for this resource, defaults to "id"
:name - the prefix for this resource. This is used for the named helper and as the prefix for the parameter in nested resources. The default value is automatically derived from the controller name, i.e. UserController will have name "user"
:as - configures the named helper exclusively
:singleton - defines routes for a singleton resource that is looked up by the client without referencing an ID. Read below for more information