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!
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.
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)
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 http://instantrails.rubyforge.org/wiki/wiki.pl 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.
Click download to get instantrails.
When you click download, you will be redirected to rubyforge.org. 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.
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:
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:
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:
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).
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:
2) Then we'll go to "Rails Applications" > "Manage Rails 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:
So in my case (and probably in yours too), we can view our application at port 3000. So just navigate to 127.0.0.1:3000, and you should get this default Rails page (plus the sidebar to the right):
We can now start building our application!
How the Framework Works
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 will be the path after the main url. For example, if we have a controller that is named "learn", it would represent yoursite.com/learn or locally 127.0.0.1:3000/learn . 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).
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 yoursite.com/learn
-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.
Lynda.com : 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.
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.
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.
This is another great resource. Once you become the next Rails ninja, you might even be compelled to edit some of the information here!
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.