Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

RESTful Routes to CRUD Mapping

Example resource: fruits

In I.N.D.U.C.E.S. route order:

URL HTTP Verb Action Notes
/fruits/ GET index INDEX when a user types localhost:3000/fruits in browser this route shows a list or index of all fruits
/fruits/new GET new NEW when a user types localhost:3000/fruits/new in browser this route shows the user a form to create a NEW fruit
/fruits/:id DELETE destroy DELETE initiates a delete request through a form submission with action = http://localhost:3000/fruits/:idOfFruit and allows the application the ability to delete a fruit
/fruits/:id PUT update UPDATE initiates a put request through a form submission with action = http://localhost:3000/fruits/:idOfFruit and allows the application the ability to Update data about a fruit
/fruits POST create CREATE initiates a post request through a form submission with action = http://localhost:3000/fruits/ and allows the application the ability to Create a fruit
/fruits/:id/edit GET edit EDIT when a user types localhost:3000/fruit/:idOfFruit/edit in browser shows the user a form to edit a fruit
/fruits/:id GET show SHOW when a user types localhost:3000/fruit/:idOfFruit shows the user an Individual fruit in the browser

Additional example resource: posts

HTTP Method
(Verb)
URI (endpoint) CRUD Operation Typical
Controller Action
Has Data
Payload
GET /posts Read all posts index No
GET /posts/:id Read a specific post show No
POST /posts Create a new post create Yes
PUT/PATCH /posts/:id Update specified post update Yes
DELETE /posts/:id Delete specified post delete No

Additional Common CRUD-less Routes (views with forms to perform create and update actions)

HTTP Method
(Verb)
URI (endpoint) Purpose Typical
Controller Action
Has Data
Payload
GET /posts/new Return view (form) to add a new post. Form submit hits CREATE route new No
GET /posts/:id/edit Return view (form) to edit a post. Form submit hits UPDATE route edit No

Routing for Nested Resources (One:Many & Many:Many Relationships)

HTTP Method
(Verb)
URI (endpoint) CRUD Operation
or Purpose
Note
GET /posts/:id/comments Read all comments for a post No payload
GET /comments/:id Read one comment for a post "Shallow" route / No payload
GET /posts/:id/comments/new n/a (Non-RESTful) OPTIONALLY display a dedicated form used to create a nested resource
POST /posts/:id/comments Create a comment for a post Needs Payload
PUT/PATCH /comments/:id Update specified comment "Shallow" route / Needs payload
DELETE /comments/:id Delete specified comment "Shallow" route / No payload

"Shallow routes are for CRUD operations where the parent's id is not needed. For example, you do not need the id of the post route to delete a specific comment - you only need that particular comment's id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment