RESTful Conventions in Rails

Mariola P
2 min readOct 4, 2020

REST stands for REpresentational State Transfer, and it is an architectural style that defines how Web standards, such as HTTP and URIs, are supposed to be used.

Rails has RESTful principles built into its core, so, whether you are utilizing the built-in view rendering system or using the application purely as an API, you will have the tools necessary to follow standardized routing procedures.

Let’s take a look at a practical example of how this works. If we want to build out a blog feature, we would need the system to have four key actions — Create, Read, Update, and Destroy — commonly known as ‘CRUD’ actions. In addition to the CRUD actions, we will also need an index page that lists out all of our blogs — that’s our fifth route. Since our users will need to have a visual interface for creating and updating records (a form for creating and another form for updating), we will need two more routes. Putting all of that together, you will see that we end up needing seven different routes. The GET routes are all routes that usually render some erb content to a web browser. These are the routes that our users will work with everyday. The POST and PATCH are the url in the form action, and the DELETE is a new type of verb.

Here is a mapping of all of the different route helpers, HTTP verbs, paths, and controller action mappings for our blog feature:

Rails maps these specific things to specific methods or “actions” as they are called in Rails. If we had a controller called BlogController, we would define these seven methods and Rails will call them automatically based on the correct route. Below is a breakdown of each of the controller actions and what it represents:

--

--