Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the parameter name using resources in Phoenix framework Router

Is there an option to make this ->

resources "users", MyApp.UserController

generate paths with parameter name other than :id ?

like image 428
NoDisplayName Avatar asked Aug 18 '15 05:08

NoDisplayName


1 Answers

Use the param parameter in the resources/4 macro:

resources "users", MyApp.UserController, param: "name"

This will generate the following routes:

  • GET /users => :index
  • GET /users/new => :new
  • POST /users => :create
  • GET /users/:name => :show
  • GET /users/:name/edit => :edit
  • PATCH /users/:name => :update
  • PUT /users/:name => :update
  • DELETE /users/:name => :delete

Following are the additional options for resources:

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

like image 175
Lenin Raj Rajasekaran Avatar answered Sep 21 '22 12:09

Lenin Raj Rajasekaran