Rolling with Ruby on Railsby Curt Hibbs
Editor's note: Curt Hibbs and Bill Walton have updated this tutorial for Rails 1.2 in Rolling with Ruby on Rails Revisited and Rolling with Ruby on Rails Revisited, Part Two. We recommend those tutorials for all new Rails development.
Also check out Bill Walton's monthly series, Cookin' With Ruby on Rails.
Maybe you've heard about Ruby on Rails, the super productive new way to develop web applications, and you'd like to give it a try, but you don't know anything about Ruby or Rails. This article steps through the development of a web application using Rails. It won't teach you how to program in Ruby, but if you already know another object-oriented programming language, you should have no problem following along (and at the end you can find links on learning Ruby).
Let's answer a couple of burning questions before rolling up our sleeves to build a web application!
What is Ruby?
Ruby is a pure object-oriented programming language with a super clean syntax that makes programming elegant and fun. Ruby successfully combines Smalltalk's conceptual elegance, Python's ease of use and learning, and Perl's pragmatism. Ruby originated in Japan in the early 1990s, and has started to become popular worldwide in the past few years as more English language books and documentation have become available.
What is Rails?
Rails is an open source Ruby framework for developing database-backed web applications. What's special about that? There are dozens of frameworks out there and most of them have been around much longer than Rails. Why should you care about yet another framework?
What would you think if I told you that you could develop a web application at least ten times faster with Rails than you could with a typical Java framework? You can--without making any sacrifices in the quality of your application! How is this possible?
Part of the answer is in the Ruby programming language. Many things that are very simple to do in Ruby are not even possible in most other languages. Rails takes full advantage of this. The rest of the answer is in two of Rail's guiding principles: less software and convention over configuration.
Less software means you write fewer lines of code to implement your application. Keeping your code small means faster development and fewer bugs, which makes your code easier to understand, maintain, and enhance. Very shortly, you will see how Rails cuts your code burden.
Convention over configuration means an end to verbose XML configuration files--there aren't any in Rails! Instead of configuration files, a Rails application uses a few simple programming conventions that allow it to figure out everything through reflection and discovery. Your application code and your running database already contain everything that Rails needs to know!
Seeing is Believing
We developers often hear the excessive hype that always seems to accompany something new. I can just imagine that skeptical look on your face as you hear my dubious claims. Ten times faster development, indeed!
Editor's Note: Bill Walton has ported this tutorial to Rolling with Ruby on InstantRails -- for a very quick installation experience.
I'm not asking you to accept this on blind faith. I'll show you how to prove it to yourself. First, I'll install the needed software. Then I will lead you through the development of a web application.
Installing the Software
We'll develop this web application on Windows. You can still follow along if you use a Linux or Macintosh system, but your screen will look different from the screen shots shown below and you will have to install software packages built specifically for your system. See the Resources section at end of this article for additional software links.
To develop this web application, install the following software:
- The Rails framework
- The MySQL database (and perhaps a MySQL GUI client)
Step 1: Install Ruby
Figure 1. Ruby Windows installer
Installing Ruby couldn't be any simpler:
- Download the latest One-Click
Ruby Installer for Windows (shown in Figure 1). As of this writing, the
- Double-click on the downloaded executable and follow the installation
instructions. Unless you have some special needs, just press
Enterto accept all of the defaults.
Note to Linux and OS X users: The Windows installer come with the RubyGems package manager already installed. Whatever means you use to install Ruby, you will probably have to install RubyGems, also.
Step 2: Install Rails
Figure 2. Installing Rails through RubyGems
Now we can use the RubyGems package manager to download and install Rails 0.9.4 (the version covered by this tutorial), as Figure 2 shows:
- Open a command window and run the command
gem install rails --remote.
- RubyGems will also install all of the other libraries that Rails depends on. For each of these dependencies, RubyGems will ask you if you want to install it. Answer "y" (yes) to each one.
Step 3: Install MySQL
Figure 3. MySQL Server setup wizard
We still need to install our database server. Rails supports many different databases. We'll use MySQL.
- Download the latest "essential" version of the MySQL Windows
installer. (Currently, that is
Windows Essentials (x86) 4.1.7.)
- Double-click on the installer (Figure 3) and accept all of the defaults, though skip signing up for a mysql.com account.
- When you reach the final panel of the installer, clicking the Finish button will bring up the configuration wizard.
- In the configuration wizard, you can also just accept all of the defaults, except that in the security panel you must uncheck the "Modify Security Settings" checkbox (Figure 4). This is because starting with version 4.1.7, MySQL uses a new authentication algorithm that is not compatible with older client software, including the current version of Rails. By unchecking this box, you can access MySQL without a password.
Figure 4. MySQL configuration wizard
Step 4: Install MySQL-Front
Author's note: MySQL-Front is not longer available. HeidiSQL is a good open source equivalent.
MySQL-Front is a graphical interface for the MySQL database. It is an inexpensive commercial application, but you can try it for free for 30 days. In this article, we will use MySQL-Front to develop our database. If you prefer, you can also just send SQL commands to the database from the command line.
- Download the latest version of MySQL-Front (currently, version 3.1).
- Double-click on the downloaded executable and follow the installation instructions (Figure 5). Unless you have some special needs, you can accept all of the defaults.
Figure 5. MySQL-Front installer