Routes in Ruby on Rails

Marshall Slemp
3 min readJul 28, 2020

When you run “rails new new_project”, rails installs a lot of stuff. You don’t need to worry about most of these folders yet but in your config folder you have a routes.rb file! This file is used to set up your routes and direct them to different controllers that will perform actions and either render a view or redirect to a new route.

A route is a url path. When you go to www.newproject.com, you’re going to the root path of that website or ‘/’. When you go to www.newproject.com/about, you're hitting the /about route. If you run ‘rails s’ to start your server, your newly installed project will have a default welcome page that looks like this!

Default Welcome page for Rails

Obviously, we don’t want this to be our homepage, so let’s change it. There are a few things we must do:

  1. We need to make a new folder in our Views directory, let’s call it ‘welcome’.
  2. Inside our newly created welcome folder, create a file called ‘index.html.erb’. Open this file and add <h1> Hello, World! </h1> in there (make sure to save the file)!
  3. The last thing we need to do is create a welcome_controller.rb file inside your controllers folder that has this code in it:
class WelcomeController < ApplicationControllerend

Now that we have created a new “view”, we need to tell our app to render it when someone goes to the root path or for us localhost:3000. Back in our config/routes.rb file we can add this line:

This line is telling Rails to send our root (‘/’) route to the welcome controller’s index action! Now, let’s restart our server and refresh the page, if everything went as expected, you should see “Hello, World!” displayed!

Now you may have noticed that we didn’t have to add any code to the Welcome controller! Thats because Rails is smart enough to understand that we want the index view from the welcome folder, simply because we said ‘welcome#index’ in our routes file. We could however define the index method in our welcome_controller and explicitly choose which view to render:

This will render a view using random_view.html.erb in the welcome folder

Let’s add an about page. In the welcome folder, create a new file called about.html.erb. Add some HTML inside that file and save it. Now in our routes.rb file, lets add the line :

get 'about', to: 'welcome#about'

Make sure you also save this file! Now back in the browser go to http://localhost:3000/about and you should see your about view (You may need to restart the server!)! As you can see, we didn’t need to add any code to the welcome_controller in order for us to see the about.html.rb view! Rails is magic!

As you can see, we define our routes in the routes.rb file and tell Rails which controller and action should handle which route! The action takes place in the controller and will usually query/manipulate some data and eventually render a view or redirect the user to a new route. If we want to render a view that is not named exactly like the action, we just need to define the method and explicitly tell rails which view to render! By default, the controller will look in the views directory for a folder with the same name as the controller, so WelcomeController will look for views in the Views/welcome folder.

What is the difference in render and redirect? When you render a page, you are saying send this view (about.html.erb) to the client. When you redirect, you are basically changing the clients path which will call a new action, this new action may render a view or redirect. Ultimately though, you will render a view down the line, or make someone extremely irritated!

--

--