Rails Guide — Part 1 All About Routes

James JingChao Yu
5 min readAug 5, 2021

--

In this mini series of blog, we will go over working with Rails, a backend framework that’s powered by Ruby. As Rails is a MVC (Model, View, Controller) design structure, each week we will explore a different component, and see how it fits into the complete puzzle.

Routes is the entry point for your app, and the first interaction your users will come into. A route is nothing more than a URL you define, and when a user navigates to that URL, they are sent to a controller action, the controller communicates with the models (databases) and takes the user to an HTML page.

Let’s see how all this works from scratch. From your terminal, create a new Rails project by running:

rails new RoutingExample — database=postgresql

Then run this command to create the database:

rails db:create

And finally you can run rails s to start the Rails server, visiting localhost:3000 on your browser and you should be this page:

Let’s switch to VS Code to take a look at the app. From config folder, find the routes.rb file, this is what you should see:

Suppose we are working on a personal website, where it would have a home projects, blogs, and contact page. Let’s first create a static home and contact page, and later more dynamic projects and blogs pages where we can do various CRUD actions.

To add a route, it’s as simple as writing:

Get is one of the five method names (get, post, patch/put, delete). Second part is the name we give to the route, last part is the mapping to the controller’s action. Notice we don’t have even have a controller yet, the name I gave here “some_controller” and “action1” are just for example purposes.

If you run rails routes in your Terminal:

home and contact routes are successfully added to our routes. And on the right hand side, it’s associated with some_controller action1 and action2.

At this current state, the app would not run, so let’s manually create a Pages controller and give it some actions.

In the App, Controllers folder, create a new file named pages_controller.rb and inside of it, create a new class and two methods like so:

Now we have a Pages controller, let’s go back to our routes and update it:

Run rails routes in the Terminal, and we can see the controller and actions have been updated:

We still need view files for all of these to work. But now would be a good time to restart the rails server as we updated our routes.

From the App folder, locate the views folder, and create a pages folder inside of it and a home.html.erb and contact.html.erb files inside the pages folder.

The structure of the files

We will put a simple <h1> tag inside of the erb files and test them out.

Going to localhost:3000/home now yields this page, which is exactly what we want:

You can really customize your routes however you want, let’s say later on we want to add another route called some-hidden-page:

Next we have to set it to a controller’s action (notice we can map it to any controller we want, not just the pages controller). Now we can go define the missing action in our pages controller:

And the last step is to create a view file of the SAME name. Since we call our action missing, if we were to name our view file anything other than missing, this would not work.

We can check the routes in our Terminal:

Restart the Rails server and navigating to localhost:3000/some-hidden-page, and you should see this:

To replace the Rails welcome page you see at localhost:3000 with one of your own, in the routes.rb file, simply call the root method:

Now notice if you go to localhost:3000, it will be mapped to pages#home

There will be times where you want a user see a 404 page not found error. Imagine the scenario where your user types random URLs in your page, like localhost:3000/hello-world, localhost:3000/account, (of course in production, localhost:3000 would be replaced by your actual domain name) instead of running into a bug or your website crashing, you can define a route to catch all wildcard routes.

Now if the user navigates to some URLs that don’t exist:

The order in which you put this route matters. If you were to put it at the very top above other routes, it would catches everything and your other routes would never be hit.

That’s a quick introduction to routes in Rails, next week we will take a look at the Controller and some of the cool stuff we can do with it! See you!

--

--

No responses yet