Rails Routes for Rookies

Tiffany Carter
5 min readMar 11, 2019

--

The past few weeks I’ve been learning how to use rails and all that comes along with it. Recently, I completed a project called Tracoon, a web app that tracks raccoons spotted in Seattle neighborhoods. One concept of the project we frequently used were the routes in Rails. While I understood how to use them in our project, I wanted to take a deeper dive to gain a better understanding.

Routes

The purpose of routes in Rails is to map incoming HTTP requests to the controller and action that should handle them. Upon receiving the request, rails will scan through each route defined in your routes.rb file to see if one matches the request verb and URL combination. Rails will send the request to the first one that matches and if it reaches the end and there isn’t a route, it will raise a no route matches error.

Image from learn.co Intro to REST

Another thing that routes can do is pull certain pieces out of the path and make them available to the controller in the params hash. When your application receives a request for GET /raccoons/:id, the :id is a placeholder for the value you enter. So if you were to send a request of localhost:3000/raccoons/6, it would locate the show action and show us the raccoon with an id of 6. We’re able to store that raccoon in @raccoon and use it in the view.

The show action in the RaccoonsController

Resources

You can make your own routes and actions, but it’s best practice to use RESTful routing. REST stands for REpresentational State Transfer and it’s an organization pattern that rails recommends using. It’s not required, but REST provides a good set of interactions that handles most common use cases. There are seven main types of actions that you should use on an object, or in our case raccoons. Below you can see the seven routes and what action they go to.

One thing to note is that many of these routes have the same URL, but they use different HTTP verbs so they can be sent to the correct action

A list of the verbs, paths, actions, and what they’re used for

You can type out your routes this way in your config/routes.rb

Rails knows that we might want to have all of these routes, so it gave us this helper method. By just saying resources :raccoons, this will give you all of the routes above.

What if you don’t want all seven? You can write to give you just the index and show routes OR you can put resources :raccoons, except: [:index], which you guessed it, gives you everything but the index route. Thanks rails!

resources :raccoons, only: [:index, :show] #this will give you just the index and show routesresources :raccoons, except: [:index] #this will give you all routes except index

Rails Routes is your friend

You can always see what routes you have defined, along with their helper methods when you run rails routes in your terminal. This really helped me understand what was happening when I was creating links in my web app, and realizing what path I needed to send it to.

Each of these has a corresponding URL helper, so if you wanted to see a list of your raccoons you can see the URL would be localhost:3000/raccoons. Likewise, if you wanted to see a specific raccoon you would type in, for example, localhost:3000/raccoons/1.

Renaming URLs

What if you have a sessions controller that logs in your user, but you don’t want the URL to be localhost:3000/sessions/new but instead want it to be localhost:3000/login? Well, we can do that! Below you can see that if you use the as: function, it will turn our path into /login

Changing sessions/new into /login

If we run rails routes, we can see that routes added a login path for us.

Rails routes shows us what our path looks like now

Nested Routes

What are they? For example, we wanted to add comments to our raccoon pages. With nested routes, this will allow us to find the comment for a specific raccoon inside our raccoon show action. So let’s add comments under the raccoons resources in our route.rb file.

Let’s run rails routes and see what routes we have now!

rails routes for our nested routes

You can see that we now have comments under our raccoon routes. By having these URLs, we now have access to two params necessary to find the requested object, raccoon_id and id(which is the comment id). Of course, nested routes can get a little out of hand if you have too many. It’s best to try and keep your nested routes from going deeper than one level.

Hopefully, this has cleared up some confusion on routes, below are a few pages that I read through to try and get a better understanding. Check them out.

Routes cheatsheet

Rails Routing from the Outside In — Ruby on Rails Guides

4 things i learned toying around with nested resources in Rails

--

--