understanding rails routes

Rails routes have confusing syntax, but they are actually quite straightforward once you understand them.

What is a route?

A rails route tells your browser what to do when it sees a certain web address. That is, you type in a url, and the route connects your browser to the controller which performs an action or renders the html view. To write a route, all you need to do is put the url on one side and the controller and its action on the other. Your routes tell the browser:

when you type in this url => look in a controller and do this action

get “/url” => “controller#action”

For organization, it’s helpful to give actions the same name as our views. For example, in one controller, you usually have an index action, which provides the backend code for the index.html.erb view.

As long as your controller actions have the same names as their views, you can think of your route as: get “/url” => “views_folder#html_file”

Let’s see how this looks in practice:

Writing Routes

To get started you’ll need a rails app:

rails new your-app-name

And a controller. I’ll call mine “Pages” for now.

rails generate controller Pages

This creates a folder in app → views called “pages,” where you can store your html files. Go ahead and make an html.erb file in “pages.” I’ll call mine “tester.html.erb.”

Let’s write a bit of html, so we can make sure our route works later on.

<h1> Holy Routes, Batman! </h1>

Before we write our route, we have to make sure the tester.html.erb exists in the controller.

Now that we have an action in our controller, (and a file in our views), we are ready to write our route!

Go to config → routes. Because we’re just going to load the page, all we need is get request, so we’ll start with that.

get. that’s it.

Remember, the left side is the url. Since we’ll be loading our tester page, let’s call the url “/test.” This means that to pull up the page we want, all we have to do is add “/test” to the end of your domain. For now, that’ll be “localhost:3000/test”. (If we purchase a domain, it will still work as “www.mywebsite.com/test” or “moneybags.org/test”).

So now we have the left side of our route: get “/test” =>

The right side will tell the computer which document to load. Since we are using the controller Pages, it will say “pages”. The # separates the controller from the action, so far it looks like get “/test” => “pages#

Then we just add the action name from our controller to end up with:

get “test” => “pages#tester”

You’ll notice that the right side is not just the “controller#action” but also the “view_folder#view_file.” This is handy to remember when working with standard routes. You’ve done it!

Run your server (in terminal: rails s or rails server), and test out your new route!

What about CRUD and URL Params and Resources?

Okay, you caught me. There’s a bit more to routes than simple get requests. There are several different types of web requests that you can put in your routes file: get, post, patch, & delete being the most common. Each of these has a different CRUD function, but the exiting thing is that, as far as routes go, they behave in the exact same way!

That is, send this url => go to this controller and execute this action.

patch/post/delete “/url” => “controller#action”

Rails 5 comes built with a shortcut so that if you can quickly build routes to the main actions: index,show,new,create,edit,update,destroy.

All you need to do is type in resources :controller_name. For example:

resources :controller_name

To see the result of this line, we can look to our terminal and run the command: rails routes. It will return:

See our “/test” url and “tester” action up at the top? All of these routes now exist for your use.

However, it’s important to remember that routes will only work if the action exists in the controller. For example, I now have the route:

get “pages/new” => “pages#new”

What happens when I put that url in my browser?

I’d also have to make a view for this, since it’s a “Get” request.

Since it’s not in the controller, the route doesn’t work.


I hope this helped make rails routes syntax a bit clearer. For more on building a RESTful CRUD app and using those web requests as mentioned before, check here. [Link to be added later].