Learn Ruby on Rails from Scratch: Week 1


Ruby on Rails. . .by now most people have heard the hype about it. It promises more effective code, total object orientation, and true MVC architecture to say the least. As far as my own personal experience, it has been all that and more. The code is beautiful, easy to maintain, and edit. In a recent project I was working on, it took me less than 10 hours to do the application, as compared to at least double that if I was writing in PHP.

I've heard a lot of web developers that know other languages, usually ASP or PHP, that want to learn Ruby on Rails, but just don't know where to get started. From personal experience, I know it is very different from PHP. Personally, I learned Ruby on Rails less than a year ago, and have had few to no problems with it so far. If you haven't already, I highly recommend hopping on board with the up and coming web framework, Ruby on Rails!

Mission Statement

In this series of tutorials, I hope to take you from knowing nothing about Ruby on Rails (if that's true), to being able to write fully featured web applications in it. I remember what it was like to learn it. I couldn't find any series of tutorials that took you through all of the essentials in plain English and in an order that was benefiticial to me, the learner. Although there are very good resources out there, they need to be combined with others as well. At the bottom of this tutorial, I'm also going to list resources that I have found useful in the past as well.

This series will be similar to the PHP from scratch series, but with Ruby on Rails.There will be one published every week, gradually getting more advanced.

This Tutorial

I warn you, this first tutorial might be a little dry, but it's necessary for learning RoR (let's just call it that from now on!). This week, we're going to cover setting up the framework and web server as well as how the framework works. We're going to set the necessary foundation for learning RoR. This is where many web developers that want to learn RoR fail.I promise we will be doing much much more exciting things next time!

What is Ruby?

Ruby is the language that the framework rails extends. It is very object orientated. It's syntax is inspired by Perl's, and therefore is very similar. It is also similar to Python, Lisp, Dylan, and CLU. To quote Wikipedia:

 "Ruby was conceived on February 24, 1993 by Yukihiro Matsumoto who wished to create a new language that balanced functional programming with imperative programming.According to Matsumoto he 'wanted a scripting language that was more powerful than Perl, and more object-oriented than Python.That's why I decided to design my own language'. "

It was most popular in Japan, that is until the framework Rails came out. Ever since, it has continued to grow at a phenomenal rate everywhere.

What is Rails?

Rails is the famous framework that works off of ruby. It's user base is constantly growing. Developed by David Heinemeier Hansson of 37signals for basecamp, it was released as open source in July 2004. It uses MVC (Model - View - Controller ) architecture. If you want a brush up on MVC, be sure to check out the MVC tutorial. The latest release is 2.1.0. There were some significant changes in the v2. This includes the famous scaffolding feature of v1. It's changed a little bit in version 2.

Concepts of Ruby on Rails

Ruby on Rails follows several principles to help keep it's code sleek and clean. You should try to live by these principles while you're working with RoR to get the most of it. For one, it follows the Agile programming term of DRY (Don't repeat yourself). Obviously, this means that you only write your code once in a certain consistent place. For example, if you need the code to do a certain thing, then you'll put it in a certain place every time.

The second concept that RoR follows is CoC (Conventions over Configuration).This pretty much means that ruby assumes a lot, and explains most of the automatic defaults you'll see ruby generate for us. Instead of you always having to configure everything, in every single project, ruby does it for you automatically.Then, if you want to do something that's unconventional, you simply override the sensible defaults that RoR already has in place.This generally leads to less code having to be written.

Advantages of Learning Ruby on Rails:

  • It is pretty easy to learn compared to other languages
  • Completely Object Orientated
  • MVC Architecture
  • You don't have to write as much code
  • Very extendible
  • Open Source
  • The community is extremely helpful in answering questions
  • Despite being a relatively new framework, it is pretty fully featured, and has very few bugs.

Disadvantages of Ruby:

  • Processed different, so the code can be a little bit slower on servers that aren't really built to handle ruby code

I've only heard this claim, but more and more servers are becoming better equipped for Rails as it continues to grow anyway so it's becoming less and less of a problem.

A Ruby File

A ruby file has the extension of .rb. These files will usually contain no HTML. This is an excerpt of a ruby file (specifically a controller) from a game engine I recently made. As you might be able to see. It pulls data from a database, and lists games according to the variables category and title:

class GameController < ApplicationController

	def game_list
@title = '3'
@category = 'Action'
@games = Game.find(:all,
:conditions => ["title LIKE ? AND category = ?", '%' + title + '%', category],
:order => 'title, category ASC',
:limit => 5, :offset => 0)
end end

An embedded Ruby File

Like many other web languages, Rails allows you to embed ruby with HTML. Seki Masatoshi wrote a program called ERb, embedded ruby, which scans all .rhtml files (embedded ruby files), until it comes across either <% Ruby Code that is only processed -%> or <%= Ruby Code that is then outputted %>. It then processes the ruby code and fills in the blank if neccesary.

Here is an example of Embedded Ruby:

<% title = "Nettuts" %>
<% header_text = "Ruby on Rails Tutorial" %>
<title><%= title %></title>
<h2><%= header_text %></h2>
<p>This is an example of a .rhtml page. </p>

This is how the user will get it:

<title> Nettuts </title>
<h2>Ruby on Rails Tutorial</h2>
<p>This is an example of a .rhtml page. </p>


Installing Ruby on Rails For Windows

Windows and OSX users are both very lucky. Both operating systems get an all-in-one installer/manager program that installs ruby, rails, and the server.For windows, you have the option of Instant Rails. Go to to download it. I personally have used this, and have had no major problems with it.Trust me, it is much easier to do this, then install everything separately.

Instant Rails

Click download to get instantrails.

When you click download, you will be redirected to There you can choose the latest version. At the time of this writing, 2.0 was the latest. Also at the time of this writing, a lot of popular hosts that supported ruby, only supported 1.8.x version. There are considerable differences between version 1 and 2. So it's up to you which version you'd like to install. In this tutorial I'll download version 2.0.

Instant Rails Download

Now that it's downloaded, extract it to a directory that contains no spaces.

Installing Ruby on Rails For Other OS's

OSX also has the good fortune of having an all in one Ruby on Rails manager, locomotive. For OSX, and other Operating Systems, you can find a very good resource at Ruby on Rail's wiki.


Getting Started with the Framework

From now on, I'm going to be using Instant Rails. It isn't very different from any other set up so if you're using another technique you should still be able to follow along easily. If you run into any problems, be sure to let me know in the comments and/or check out the resources.

So now that you have all of the files needed in place for your setup. Start it up! For windows, go to the directory you put Instant Rails, and run InstantRails.exe . You probably will get this message, but for your directory, which you can press 'yes' for:

Instant Rails Message

Now that Instant Rails is running, you can do several things. Right now, you only need to able to:

- Open a console window

-Create a new project.

- Start the server for a current project

We'll go through each action separately.

Open up a console window

1) Click on the upper left icon:

Instant Rails UI

2) Go to "Rails Applications" > "Open Ruby Console Window"

3) We now have a console window open. Here you can do many things in ruby like generating new projects, controllers, and other tasks that we'll cover later. Now we're going to use this Console Window to create a new application.

Creating A New Application

1) Now we're going to type in "rails nettuts", where nettuts is the name of your application. Choose the name carefully, as you'll be using it for the rest of the application. Since we're not really doing anything particular yet, we'll just choose nettuts as our name:

Console Create Nettuts

2) It will now generate all of the necessary files for the application. These are the files that the framework uses and needs. It will help provide the CoC (Conventions over Configuration).

Console Create Nettuts

3)We now have a Ruby on Rails application generated!

Running an Application

1) To run our application, we need to go to Instant Rails and click on the icon again:

Instant Rails UI

2) Then we'll go to "Rails Applications" > "Manage Rails Applications..."

Manage Applications

2) You can see Instant Rails ships with two projets already. The cookbook is a pretty neat demo of what you can do easily with Rails. I highly encourage you to check it out. And then after you learn a little bit more about Rails check out the source code. You'll be surprised at the simplicity! Anyway, check the box next to nettuts, and click "Start with Mongrel" . Your firewall might object, but allow it. A console window should pop-up with the port number and other useful information:

Manage Applications

So in my case (and probably in yours too), we can view our application at port 3000. So just navigate to, and you should get this default Rails page (plus the sidebar to the right):

Manage Applications

We can now start building our application!

How the Framework Works

MVC Architecture

Rails runs off of MVC Architecture. Basically that means we break up our code into three sections; the model, the view, and the controller. If you'd like to learn more about MVC Architecture be sure to visit the Nettuts MVC introduction tutorial. In this intro to Ruby on Rails, we'll only cover the basics of the controller:

The Controller

The controller will be the path after the main url. For example, if we have a controller that is named "learn", it would represent or locally . Each controller relies on many specific files, therefore to create a controller, you need to generate it via the console. We're now going to create a new controller, learn:

1) First, we open up a console window via Instant Rails, or which ever setup you're using.

2)Next we navigate to the current application. Since we are currently one level above our application, we type in "cd nettuts" (change directory to nettuts).

cd nettuts

3)Now that we're in the application, we need to access a script that will generate the controller. If you examine the file structure of the app, you'll notice that there's a script folder. And inside the script folder, there is a generate file. We need to target that, and run it.


4) We'll target it via the console. Notice the other scripts as well, they all have their purpose. Anyway, in windows type in "ruby script/generate controller learn" to generate a controller called learn. If I remember right, you don't need the ruby in other Operating Systems. The console will now generate some files. Check these out in an explore window if you want. The two most important files here are the controller learn, which was just one of several files created; but notice also that a view called learn was also created. These are the sensible defaults that rails comes with to limit the amount of work involved.


So how does a controller work?

Well let's take it step by step.

-User requests

-Rails automatically looks in the servers directory for app/learn_controller

-Rails scans the .rb file for an index file's definition

-Rails does what the definition tells it to do, and eventually creates or redirects to a view.

-This view (learn), is then sent back to the user.

Don't worry if this isn't quite making sense. Next tutorial we will go in more depth into controllers. For right now, just be familiar with how to create a controller, and that they exist.


  • : Ruby on Rails Essential Training

    Ruby on Rails aims to make building web applications simpler. In Ruby on Rails Essential Training, expert instructor Kevin Skoglund focuses on the same goal. Kevin explains the complete process--from understanding the fundamental concepts behind any Ruby on Rails project to creating full-featured, easy-to-maintain applications using the Ruby on Rails framework.

    Visit Article

  • Developer's Zone

    Tutorial's Point Ruby on Rails Series

    This is an incredible series of Ruby on Rails tutorial, which covers a lot, but it's sometimes hard to follow.

    Visit Article

  • Ruby on Rails

    Ruby on Rails

    This is Rail's official site. If you want to get excited about Rails, go here! Here you can download the newest version, get the latest news, and find some other resources for learning Rails.

    Visit Article


    Rails Wiki

    This is another great resource. Once you become the next Rails ninja, you might even be compelled to edit some of the information here!

    Visit Article

Be sure to check out as many of these resources before the next tutorial.

Next Week and Final Words

Well, I hope you were able to survive through all of that information. Trust me, you'll be glad you did. Be sure you get the overall grasp of this tutorial. As we go on, most of your questions will be answered, and you will start feeling more "enlightened", on what Rails is. And I promise next week will be more fun! Next week we will cover a little bit more of the framework, and start getting into ruby syntax, and a little more of what you can do with rails. If this tutorial helped you, please Digg it!

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.

Related Posts
  • Code
    Code Generation Using T4Dotnet wide retina preview
    Learn the advantages and disadvantages of using code generation via T4 templates.Read More…
  • Code
    Validation and Exception Handling: From the UI to the BackendProcedural to oop php retina preview
    Sooner or later in your programming career you will be faced with the dilemma of validation and exception handling. This was the case with me and my team also. A couple or so years ago we reached a point when we had to take architectural actions to accommodate all the exceptional cases our quite large software project needed to handle. Below is a list of practices we came to value and apply when it comes to validation and exception handling.Read More…
  • Code
    Kickstarting Your Rails EducationRails education retina preview
    It's been a long time since I last coded on the server-side. In fact, if you've read some of my tutorials, you may have noticed that I use ColdFusion as my application server. While ColdFusion still works great, it definitely doesn't have the panache and coolness of newer server-side technologies like Ruby on Rails. Wanting to be a bit more modern, I've decided to jump on the Ruby on Rails train. Both Ruby and the Rails framework are proven technologies that are stable and widely embraced so I think it's a great direction to head to in my server-side renaissance.Read More…
  • Code
    JavaScript & AJAX
    Introduction to Sails.jsSails preview 400 2
    Sails is a Javascript framework designed to resemble the MVC architecture from frameworks like Ruby on Rails. It makes the process of building Node.js apps easier, especially APIs, single page apps and realtime features, like chat.Read More…
  • Code
    JavaScript & AJAX
    Working With IndexedDB - Part 2Indexeddb retina preview
    Welcome to the second part of my IndexedDB article. I strongly recommend reading the first article in this series, as I'll be assuming you are familiar with all the concepts covered so far. In this article, we're going to wrap up the CRUD aspects we didn't finish before (specifically updating and deleting content), and then demonstrate a real world application that we will use to demonstrate other concepts in the final article.Read More…
  • Code
    Building Ribbit in ScalaRibbit scala retina preview
    In this tutorial we will implement the Ribbit application in Scala. We'll be covering how to install the Play web framework, a NetBeans plugin for it, and finally the code in Scala. If you are new to Scala, check out this previous tutorial which will help you set up your environment and provides you with a general platform that you can build upon. Even though the essence of Ribbit is to create/send/read Ribbits (our version of tweets), we will spend a large part of this tutorial explaining how Play works, authentication, and persistence. After these are in place, the rest becomes much easier. We will also implement ribbit creation, submission and listing out all ribbits. Following someone, advanced user settings, and direct messages will be an extra assignment for you to complete on your own. I am sure if you manage to follow along with this tutorial and create Ribbit as explained below, these three functionalities will be easily accomplished as homework.Read More…