Rails Routes

Jesse McReady
4 min readSep 3, 2018

--

How are we able to visit https://www.youtube.com/user/FirstWeFeast and be brought to First We Feast’s page that holds their recent videos and playlists? How can you search for something on Google, such as “Ruby on Rails Routing” and be given pages upon pages of websites that are relevant to your search? Routes are used in all programming languages that deal with web development. I am going to assume you know nothing about routes and requests, but if you have an idea of those things and want to just jump into how to write them in Rails, scroll down to the Routing .

Parts of a URL

Let’s take a look at https://www.youtube.com/user/FirstWeFeast again and break down its parts. A URL is made up of a protocol, a domain, a path, and parameters.

  • https : This is the HyperText Transfer Protocol Secure . As the name suggests, this is the protocol used to transfer HyperText (HTML) . The Secure part just means that requests are encrypted.
  • www.youtube.com : This is the domain name, a string that either holds, or is a alias for, an IP address. So when you navigate to YouTube, you are actually navigating to YouTube’s IP address. It would just be really annoying and tedious to remember the IP for each website. Imagine going to https://127.123.456.789 just to get to YouTube.
  • /user : This is the path we are taking. The path will differentiate between the resources we want to see. This changes based on what you are doing on the webpage. For instance, YouTube has “/user” when you go to a users webpage, “/results” when you search in the search bar, and “/watch” when you navigate to a video.
  • /FirstWeFeast : This is the parameter we pass to our path. It indicates specifically what user we are looking for. So when combined, our URL is requesting www.YouTube.com through the HTTPS protocol and then asking for the user whose username matches FirstWeFeast.

Types of Requests

Before we jump into how to route, we have to talk about the different types of requests we should be able to handle. You may have heard of the term CRUD, which is a development term that says your app should be able to Create Read Update Delete resources. In web development, our CRUD actions are mapped to different requests.

RESTful Routes

Now that we have an idea about requests, lets talk about RESTful Web Services, which is a style of creating routes in websites that make sense. For instance, lets say you are writing the next Twitter, and we need routes to deal with Tweets. Our main path will be “/tweets” because it makes sense that our website should have a path for tweets, just like YouTube has a path for users.

The “/:id” is the parameter we pass in with our path so we know which tweet we are looking for, since all tweets will be stored with a unique ID. Now, our actions have different names, they point to the RESTful Action we should be taking when we take in these paths. “Index” will display all of our tweets, “new” shows a form that will create a tweet and would use a POST request when the form is submitted, “show” will show the page for the specific tweet we want (like user/FirstWeFeast), “edit” will show a form to update an existing tweet and will call PUT/PATCH to update, “delete” is an action you attach to a button or something like that so you can click and delete the specific tweet.

Routing

Ok so you know about URLs, Requests, and REST, now what? We are going to implement these ideas into a Rails app. All of our routes will be going into our config/routes.rb file. The first route we will take care of is the route when users visit just our website, no path included. This is called our root.

As for our RESTful routes, we can do it one of two ways, by either individually declaring each path or by allows Rails to work it’s magic.

You may realize that sometimes you might want a customized route name. Rails allows us to do that with the “as” keyword.

Just make sure that you have a controller and action to match the routes you make. In our example we will need a TweetsController with an index and show methood.

Final Thoughts

This post is dragging on a little longer than I would like it, so in the future I’d like to write a post about more advanced techniques with routing. Rails is a really powerful tool to quickly make websites dynamic and scalable. Learning how to route and connect your models is perhaps the most important thing to learn and get down before worrying about how your website looks. Hopefully this post cleared up some things, but you can always ask questions in the comment section and I will do my best to get back to you.

Originally published at gist.github.com.

--

--