DEV Community

loading...
Cover image for Let's talk RESTful Routes

Let's talk RESTful Routes

lupitalee profile image Lupita Rivera Updated on ・2 min read

While starting on my Sinatra project I was literally lost on my routes felt like I was inside a maze wandering around without finding an end. here where I realized I need to go back to my reading and review what I had learned in the lessons. join me and let's figure it out together the way out of this maze.

Alt Text

Let's start with what is REST? (Representation State Transfer) is an architectural style for defining our routes. It is a way of mapping HTTP routes and the CRUD functionalities. then what are Routes? are the code that is responsible for listening and receiving requests and then deciding what to send back. and for last what is CRUD? when building an application, we want to provide the four basic types of functionality. There must be a way to Create, Read, Update, and Delete resources.

for example, The internet would be a really confusing place without a convention for how to handle URLS - to delete an Instagram photo might be www.instagram.com/delete-this-photo, but Twitter might be www.twitter.com/remove-this-tweet. Without a specific convention to follow, it would be hard to create new content, edit content, and delete it. this is where RESTful conventions come in, to provides a pattern where users and developers can consistently manipulate data and be on the same page.

The 7 RESTful routes. let's use my code routes as an example,

Alt Text

I also noticed that browsers behave a little strangely as it relates to PUT, PATCH and DELETE requests, in that they don't know how to send those requests.so in order to get them to send we need to hide the request inside a Form, that delete and edit need to be submitted via POST requests. see an example below.

Alt Text

the form must include a hidden input field that will submit our form via delete. The second line above is what does this for us.

These 7 routes will get any web app started. You’ve got all the basics to use CRUD actions and the RESTful routes to create, read, and manipulate data.

Alt Text

By following this pattern, you don’t have to reinvent the wheel every time you build a new CRUD app. The routes and method names have already been decided is like following line exits inside the maze that someone already left behind for us to follow that way we can easily go in and out without getting lost.

thank you for joining me and together we were able to find the exit to start coding!

Discussion (0)

Forem Open with the Forem app