Demystifying Rails – Routes, Views, Controllers

Ruby on Rails First App

In our General Assembly course we spent the first 4 weeks learning about the Ruby language, and now we are spending the next 6 weeks building in Ruby on Rails. I have previous experience practicing with rails – from Treehouse, RailsGirls and other tutorials – but I quickly learned that those tutorials weren’t that helpful for helping me understand Rails. For example, all those previous tutorials used ‘scaffolding’ which is a whole lot of Ruby magic and shortcuts, and you don’t really get to understand what is happening. This is bad because then later on if you want to customise it, you don’t know how.

At our General Assembly course, we are learning the how behind Ruby on Rails, going through every little process to better understand it and demystify it. I know I will need to refer back to my notes in the future – so I am putting them here for my own sake:

Create a New Rails App – First Route, Controller and View

To create a new rails app, you simply need to type into the terminal

$ rails new appname

Where appname is obviously whatever the appname is. For this example,  I will say I am making a books app, and so the command would be

Step 1:    $ rails new books_app

It became clear to us later on that we shouldn’t name our app just ‘books’. Because Rails will then make a model called ‘books’ as the default model, and if we didn’t want to use that model, then we would be in a bit of trouble – therefore it is better to name your app something like books_app.

When you run rails new books_app, rails will generate a whole lot of files and folders for you – move into the folder you just created, to view those files

Step 2: $ cd books_app

You should see a whole lot of files, including gem files, db, app, config and more. I like to open this whole folder up in sublime text. Already you have made the basis of your app, and you can view it in your browser by starting up the rails server

Step 3: $ rails server

Note that to get out of rails server, you can do ctrl + c But if you only want to get out of it so that you can use the command line again, you should probably just open a new terminal window.

Step 4: In your browser, go to http://localhost:3000/ which is the port your Rails app will be responding through.

You should get a page saying “Welcome Aboard, You’re Riding Ruby on Rails!” Then, it will have a few tips on the steps to make your app, but the order is a bit different to what we did in our class. First, we made a route to our index page, so that that Welcome page is gone.

Step 5: Open your Config/routes file and create a new line under the first (existing) line saying

root 'welcome#index'

What this will do is, when someone goes to the root URL,  they will get sent to the Welcome controller’s, Index action. So, this means it will go to the controllers file called WelcomeController (which will have a class called WelcomeController), and then it will use the Index action in that WelcomeController class.

If you save your files and go back to your browser and refresh, you should see an error message saying uninitialized constant WelcomeController – because you haven’t made it yet!

Step 6: Go to app/controllers and make a new file called welcome_controller.rb

In your WelcomeController you want to make a class called WelcomeController, and give it at least one method called index. So it would be like this

class WelcomeController < ApplicationController

def index


Here you can see that the WelcomeController is inheriting from something called ApplicationController – which is a part of Ruby on Rails, and it has to do this  - it inherits some Rails magic from here too, you see.

Now, if you refresh your template again, you will see another error – this time saying you have a missing template. That’s right – you have the controller in place, but you don’t have anything to view it with – so you need to make a view (or you might call it a template).

As an aside – I love how Rails will tell you what is missing with such great messages, so you can easily (well not easily really) step through the process.

Step 7: Go to app/views and make a new folder called welcome

That’s right, a folder, not a file, because you might want to put a number of different view template files in this folder for the one controller. The naming of the folder to match the controller is also really important – because the Rails magic depends on you using a certain naming style, so it knows where to look

Step 8: In app/views/welcome create a new file called index.html.erb

This file name extension shows that in this file you can write both html and erb (I think it stands for Embedded RuBy) – so you can use html to make the page look however you like, and you can use Ruby to pull things in dynamically.

Write something in that file – for example some html like

<h1>Hola Ruby</h1>

And if you have saved everything, then you should be able to go back into your browser, hit refresh, and see what you wrote!!


I think that’s enough for this post. Like we did in class, I will separate out Models as a separate post.



Leave a reply