Photo by Cookie the Pom on Unsplash

RESTful Routing CRUD actions in Ruby on Rails

Ajak Cyer

--

RESTful routing is a way to make navigating our web applications easier. It gives us a simple and consistent design pattern for implementing CRUD actions in our routes.

Rails has a simple way of implementing this. When we create our new rails project folder by running rails new. It comes with many folders that seem a bit overwhelming at first. Luckily, we only need to focus on a few folders to implement our RESTful routes: app, and config.

Implementing our routes

The example I’m going to be using is a web application for Creating, Reading, Updating, and Deleting an article. A great website to use for seeing the paths and http verbs for our RESTful routes is Restular. It makes things a bit easier when you can see it.

Image from Restular.com

In our config folder, our routes.rb file is where we assign the routes with it’s HTTP verb and controller-action. Rails comes with a built in method called resources that we use to create RESTful routes, it maps our routes to CRUD actions on our controller.

By running `rails routes` on our terminal we see the routes created by the adding that simple line of code onto our config/routes.rb file.

This is very similar to what we see from the Restular website. Take notice of URI patterns that have /articles/:id these link to specific articles by their id#

Under the Verb column, we have HTTP request verbs: GET, POST, PATCH, PUT, and DELETE.

GET

Every GET request should get a page for us to see. This coincides with the CRUD action — Read. This type of request shouldn’t change our data but only show us something.

  • articles#index routes to /articles a view page that shows a list of all articles.
  • articles#show routes to /articles/:id — a view page that shows a specific article.
  • articles#new routes to /articles /new a view page with a form to create an article.
  • articles#edit routes to /articles/:id a view page with a form to edit a specific article.
This code is written in our apps/controller/articles_controller.rb file to link an instance variables to its respective view page.

POST

POST requests are for adding a new row into our Articles table and creating associations if there are any. This coincides with the CRUD action — Create. This type of request is meant to change our data and a view page is not needed. We can write the code in our articles_controller.rb file for creating a new article.

  • articles#create routes to /articles as a post request and adds a new article row to our data.
  • We get our parameters to create a new article from the new article view page form. When we click on the submit form button it sends the information entered to our articles#create action.
This code is written in our apps/controller/articles_controller.rb file. An instance variable is not needed since we are not calling it onto a view page.

PATCH/PUT

PATCH/PUT requests are for updating a specific row in our Articles table. This coincides with the CRUD action — Update. This type of request is also meant to change our data and a view page is not needed. We can write the code in our articles_controller.rb file for updating an article.

  • articles#update routes to /articles/:id — as a PUT/PATCH request and updates a specific article row on our Articles table.
  • We get our parameters to update an article from the edit article view page form. When we click on the submit form button it sends the information entered to our articles#update action.
This code is written in our apps/controller/articles_controller.rb file. An instance variable is not needed since we are not calling it onto a view page.

DELETE

DELETE requests are for delete a specific row in our Articles table. This coincides with the CRUD action — Delete. This type of request is also meant to change our data and a view page is not needed. We can write the code in our articles_controller.rb file for deleting an article

  • articles#destroy routes to /articles/:id — as a DELETE request and deletes a specific article row on our Articles table.
  • This can be a button or a link that has a route to the articles#destroy.
  • This can be a button or a link that has a route to the articles#destroy. When we click on the link or button the code we wrote in that action will run.
This code is written in our apps/controller/articles_controller.rb file. An instance variable is not needed since we are not calling it onto a view page.

RESTful routes give us an easy way to create our CRUD routes without having to actually write all seven routes individually.

--

--

Ajak Cyer

Software Engineering Student at Flatiron NYC. Learning day by day 💻