Anatomy of a Custom Rails Route

Karan S. Chauhan
4 min readAug 19, 2020

“What sets this framework apart from all of the others is the preference for
convention over configuration making applications easier
to develop and understand.”– Sam Ruby, ASF board of directors

www.giphy.com (anatomy)

It was finally the start of the second module of Flatiron’s immersive software engineering bootcamp. As a student I was eagerly awaiting instruction on how we can use Ruby on Rails to create web applications through the Model-View-Controller (MVC) design pattern. I had just learned about class models and the varying associations between them. Now, building on that knowledge, I was taught about restful actions to incorporate in the Controllers of the application(s). These restful actions were the following methods that would be included within the controller:

www.github.com

Following these methods, I would need to configure the routes.rb file, within the config folder. What’s the purpose of the routes.rb file?

Well, the routes.rb will recognize URLs and generate the applicable paths on its own, therefore reducing the necessity to manually code yourself. It’s a time saver!

www.giphy.com (shortcut)

Because of the simplicity of rails, instead of creating paths for each of the actions above separately into the routes.rb file, I can simply use a rails route helper method called “resource”. Let’s look at the example below:

On lines 3–5, the helper method “resource” is doing the heavy lifting. Instead of hardcoding the paths for the Index, Show, New, Create, Edit, Update, and Destroy methods (from the controller), “resource” does it for me. Short ‘N Sweet!

What about on line 6? Well, for this particular assignment, I had to create a custom method in one of the controllers. The #like method.

Since a custom method was created, a custom routes path must also be created. That’s where the code on line 6 comes in.

It’s easier to understand once it’s split into parts:

Part 1:

This initial part of the code represents the url the user will enter into the browser and send a patch response to. Remember, a patch response acts as an update. In this particular assignment, the use of the #like method as well as the specific route, was to update the amount of likes a blog post will have. Similar to how a like button would work on any social media platform.

Part 2:

This code represents the specific controller being referred to as well as the specific method. For this example, “posts” is to the left of the #, meaning the routes will go to the posts controller. On the right of the # is “like”, this is the method within the posts controller that will be executed.

Part 3:

This is simply a nickname given to the route, in this case, I named this route “like”.

General Anatomy:

<http verb> ‘/path’, to: ‘<controller>#<custom method>, as: ‘<nickname>’

Fill in the values in between the <> and there you have it. Your custom route(s) is complete!

Resources:

https://medium.com/@zerminaejaz/understanding-ruby-on-rails-explicit-routes-and-creating-custom-routes-ec682b64b15

--

--