Rolling 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:

* Ruby
* The Rails framework
* The MySQL database (and perhaps a MySQL GUI client)

Step 1: Install Ruby

Ruby Windows installer
Figure 1. Ruby Windows installer

Installing Ruby couldn't be any simpler:

1. Download the latest One-Click Ruby Installer for Windows (shown in Figure 1). As of this writing, the latest is ruby182-14.exe).
2. Double-click on the downloaded executable and follow the installation instructions. Unless you have some special needs, just press Enter to 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

installing Rails through RubyGems
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:

1. Open a command window and run the command gem install rails --remote.
2. 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

MySQL Server setup wizard
Figure 3. MySQL Server setup wizard

We still need to install our database server. Rails supports many different databases. We'll use MySQL.

1. Download the latest "essential" version of the MySQL Windows installer. (Currently, that is Windows Essentials (x86) 4.1.7.)
2. Double-click on the installer (Figure 3) and accept all of the defaults, though skip signing up for a mysql.com account.
3. When you reach the final panel of the installer, clicking the Finish button will bring up the configuration wizard.
4. 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.

MySQL configuration wizard
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.

1. Download the latest version of MySQL-Front (currently, version 3.1).
2. 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.

Please click one of the following ads by Google to go to the next page...

Top 12 Ruby on Rails Tutorials

  1. Rolling with Ruby on Rails - Curtis Hibbs of ONLamp.com offers his first excellent introduction to Ruby on Rails. This is the article that got me really excited about RoR.
  2. Rolling with Ruby on Rails, Part 2 - The sequel to Curtis Hibbs excellent series of articles.
  3. Four Days on Rails (PDF) - a great tutorial that is broken down into simple tasks that you can do over a four day period. To be quite honest, this tutorial only takes about 2 hours, but nonetheless it is very well organized!
  4. Really Getting Started in Rails - Amy Hoy has a great tutorial that not only covers RoR, but also introduces the reader to many of the basic concepts of the very cool Ruby scripting language.
  5. Tutorial in Ruby on Rails - is a basic tutorial aimed at newbies.
  6. Fast-track your Web apps with Ruby on Rails - IBM jumps into the sandbox with an excellent (as usual) tutorial to get you on your feet fast.
  7. Getting Your Feet Wet With Ruby on Rails - Talking about getting on your feet fast, this one from Webmonkey promises to get them wet too!
  8. How to make a todo list program with Rails - Another excellent introductory tutorial that actually helps you build something useful!
  9. Ajax on Rails - Curtis Hibbs offers part 3 of his look at RoR
  10. Many to Many Tutorial for Rails (PDF) - is a nice document that begins to delve into some of the more complex parts of web application programming, but in fine Ruby on Rails manner, it’s really not too complicated!
  11. Distributing Rails Applications - A Tutorial - So now you’ve built your RoR application, how to you push it to a production server? This tutorial covers the bases.
  12. Installing Ruby on Rails with Lighttpd and MySQL on Fedora Core 4 - and of course this list wouldn’t be complete without a shameless bit of self-promotion, this tutorial promises what it says. Other install tutorials can be found here, here and here!


Please click one of the following ads by Google to go to the next page...

You don't have to be a geek to unlock iPhone (part II)

Step 3: jailbreak

Click on the Home button of the iPhone (the big and the only button at the bottom iPhone)
Do the Slide for Emergency Again
Now do the *#307# to get into the contact Screen Again
Tap your contact, and select the Jailbreak contact. Safari will open up and take you to the url: http://jailbreakme.com
Scroll down and select AppSnap
Safari will crash
Please wait a few minutes and the phone will ring, answer it and after about 2, 3 minutes, the phone should be freezing up
Go ahead and turn it off and turn it back one, the iPhone should now be activated (you can use wifi, iPod...but can not make call yet).


Step 4: install application and unlock the iPhone

Slide to unlock, you will see the icon at the bottom left which is Installer, run that. Make sure wifi is still connected
It might ask you to update, go ahead and update to the latest version of Installer.

Now go to Install tab, find Sources, install the Community Sources

Still in the Install tab, find System, install BSD Subsystem

Then go to Sources tab at the bottom, click on Edit button at the top right, then Add

Add this source: http://i.unlock.no.If it doesn't work, try adding this source too: http://rep.frenchiphone.com

Once added, do Refresh Sources

Go back to Install tab, find All Package, you should find anySim 1.1 program, install it

Restart your iPhone

anySim will shows up at the Home Screen

Run anySim and it will unlock your iPhone now.

Once the iPhone is unlocked, you can go back into the program Installer and click on Uninstall tab to uninstall the anySim program.




Please click one of the following ads by Google to go to the next page...

You don't have to be a geek to unlock iPhone (part I)

Unlock iPhone with firmware version 1.1.1

Overview

The purpose of this tutorial is to help user unlock their iPhone with firmware version 1.1.1. If you have an already unlock iPhone with version 1.02 and would like to update to version 1.1.1, you need to restore your iPhone first, see the restore tutorial here.

Unlock

Step 1: dial the magic number *#307#

Do the Slide for Emergency. The type in: *#307# and click on Call.
Your phone will ring, press Answer. If you don't get the Answer screen, delete the *#307# and type 0 then press Call
Press hold
Your phone will ring again but this time press Decline, the Contacts Screen will shows up.


Step 2: add fake contacts

Click on Contacts tab at the bottom
Click on the Add button on the top right corner
Select the firstname, lastname box, type in Wifi for the firstname
Select the URL, type in: prefs://1F then go back to the Contact Screen
Add a new Contact
Type in Jailbreak for the name
Type in http://jailbreakme.com for the URL
If you done it correctly, your screen should now look like this:


Step 3: select wifi network and get ip address

Now select the Wifi contact, and click on the Homepage link which is prefs://1F. This will get you to the Settings Screen where you can set your Wifi setting.
Make sure you get an IP address before you leave this screen


Click on Wifi-Networks button at the top left and then click on Settings button. This will take you back to the Settings Screen, go ahead and change the Auto-lock value to Never.



Please click one of

Using Python to run W3C Validator

Validating an entire site

Periodically you may want to make sure your entire website validates. This can be a hassle if your site is big. In this article we introduce a few python scripts which will help us do mass validation from a list of links. We will also modify the W3C validator to work the way we want.

Recently I had a siutation where I wanted to validate a large collection of pages. A customer has a rather large site and more than 100 editors are invloved in content creation. Since it is possible to create invalid html in their CMS tool there is a need to check the site regularly. This code could also be the foundation for a continuous testing framework in a GUI development project. In short it includes:

  1. Installing the W3C validator on a local machine.
  2. Modifying the validator to output XML.
  3. Writing a script to collect validator result for all URLs in a text file.
  4. Writing a script to crawl a web site and log all page URLs (which will be used as input to the script above).

The modified validator code and the Python scrips are available at the end of this article.

Installing the W3C validator

If you have a Mac with OS X this is an easy step. Following the excellent installation instructions on the Apple developer site I had the validator up and running on my iBook within ten minutes. Tip: If you have trouble installing the Bundle::W3C::Validator, try to install it after a reboot. It worked on my second attempt.

Modifying the validator

After installing the W3C validator and looking at the Perl code (yack!) I was surprised to find that XML output is already available! Appending ‘&output=xml’ to the query string will make the validator output the validation result as XML. It is even possible to get the result in EARL by using ‘n3′ for the output parameter!

This saved me some time. However, if the validator tries to validate a page where encoding hasn’t been specified it will produce an error in html format. For my purposes this was not good enough so I have a modified the error handling to output XML as well. I also wanted to check if the page was using Dublin Core for meta data and therefore I have included a check for the DC schema identifier.

My modifications are commented with “PKR” in the modified check script.

Some additional scripts

So, all I needed now was some simple scripts to call my local validator, parse the XML result and output data to a CSV file from which I can create statistics. The first script, massvalidate.py, takes two arguments:

  1. The name of a text file containing a list of URLs to validate. One URL per line please.
  2. The filename of the result CSV file.

The script will log information about the number of validation errors, the doctype used, web server software and the presence of DC metadata.

The second script, crawlsite.py, crawls a web site and extracts all unique URLs. The URL of the web site is passed in the first argument to the script (e.g. ‘python crawlsite.py http://127.0.0.1′). You can use this script to crawl your site and then feed the result to the massvalidate.py script.

Interesting statistics

My first trial run was to validate the start page of the 1020 public web sites listed at sverige.se. Sverige.se is the online gateway to Sweden’s public sector. The result: only 63 sites use valid html. See more statistics about doctypes used.

The next step

By adding some simple metadata to your pages (if you are using Dublin Core it may already be there) it would be trivial to have the script email the responsible developer/editor with any validation errors that were found. Please submit other ideas for development and I will add them here.

References

Please note that these scripts were written in a haste. If you find errors or have any suggestions please send them to me and I will update them as soon as possible.

  1. Installing the W3C HTML Validator on Mac OS X
  2. The W3C markup validation service
  3. The modified validator “check” script. Please replace the original “check” file with this one.
  4. crawlsite.py crawls a web site and lists links to validate (view crawlsite.py in html format). This script uses the htmldata library by Connelly Barnes. Please make sure it is available in the same folder.
  5. massvalidate.py. This script uses a list of links, feeds them to the validator and logs the result in a CSV file (view massvalidate.py in html format).
  6. The Web Design Group’s validator. This one uses a different engine compared to the W3C validator but has built in support for mass validation.


Please click one of