Ajax Hacks: Tips & Tools for Creating Responsive Web Sites

Install a cool web application framework that wraps the creation and use of XMLHttpRequest.

This hack installs Ruby on Rails on Windows; it includes information and pointers for installing RoR on Mac OS X and Linux.

You can install Ruby on Rails on Mac OS X Tiger (10.4.x) by following this detailed tutorial: http://www.maczealots.com/tutorials/ruby-on-rails/. In addition, you can install an all-in-one bundle for Mac OS X, including the lighttpd web server, the SQLite database, and RubyGems ("a packaging system for Ruby that makes it simple to deploy gems, or small applications," according to the aforementioned tutorial), from http://locomotive.sourceforge.net. The Ruby programming language itself comes built into Tiger; you can find out the version of Ruby on your system by opening up a Terminal window and typing ruby v. For information on installing Ruby on Rails on the Linux flavor called Fedora, see http://www.digitalmediaminute.com/howto/fc4rails/.

To develop a beginning Rails web application, install the following software:

  • Ruby (1.8.2 is the required minimum version for using RoR)

  • The Rails framework

  • Your database of choice, such as SQLite3 or MySQL

Step 1: Installing Ruby

Installing Ruby couldn't be any simpler:

  1. Download the latest One-Click Ruby Installer for Windows from http://rubyforge.org/projects/rubyinstaller. As of this writing, the latest version is ruby182-15.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 the defaults.

Another one-stop solution for installing Ruby on Rails on Windows is Instant Rails, available at http://instantrails.rubyforge.org/wiki/wiki.pl?Instant_Rails. It includes Ruby, Rails, Apache, and MySQL, "all preconfigured and ready to run." This is designed for Windows, but there are plans to create packages for Linux, Mac OS X, and BSD.

Step 2: Installing Rails

Now you can use the RubyGems package manager to download and install Rails 0.9.4 (the version covered by this hack):

  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. You'll be asked if you want to install each dependency. Answer "y" (yes) to each question.

Figure 7-1 shows what the installation procedure looks like in the command window.

Figure 7-1. Installing Rails through RubyGems

Finally, you'll need to install a database server, if you haven't already. Rails supports many different databases, including PostgreSQL, SQL Server, DB2, and Oracle. MySQL is a popular database used with Ruby on Rails, and it can easily be installed on Windows, Mac OS X, and Linux systems.

You can download installers for MySQL 5.0 Community Edition for Windows, Mac OS X, and Linux from this site: http://dev.mysql.com/downloads/mysql/5.0.html.

Creating an Empty Rails Web Application

Rails is both a runtime web-application framework and a set of helper scripts that automate many of the things you do when developing a web application. In this hack, we will use one such helper script to create the entire directory structure and the initial set of files to start a "cookbook" application:

  1. Open a command window and navigate to where you want to create this cookbook web application. I used c:\\rails.

  2. Run the command rails cookbook.

This creates a cookbook subdirectory containing a complete directory tree of folders and files for an empty Rails application.

Testing the Empty Web Application

A Rails web application can run under virtually any web server, but the most convenient way to develop a Rails web application is to use the built-in WEBrick web server. Let's start this web server and then browse to our cookbook application. In your open command window, move into the cookbook directory. Run the command ruby script/server to start the server. You should see something like Figure 7-2.

Figure 7-2. Starting the WEBrick server

Leave the command window open and the web server running; we'll use them as we proceed. Now open your browser, and browse to http://127.0.0.1:3000/ or http://localhost:3000/. You should see the page shown in Figure 7-3.

Unless you're following along by installing Rails, these links probably won't work for you. Don't panic127.0.0.1 is a special address reserved for the local machine.

Figure 7-3. Forthwith, Ruby on Rails

A Rails Application Directory Structure

RoR tries very hard to minimize the number of decisions you have to make and to eliminate unnecessary work. When you used the RoR helper script to create your empty application, it created the entire directory structure for the application (see Figure 7-4). Rails knows where to find things it needs within this structure, so you don't have to tell it. Remember, no configuration files!

Figure 7-4. Where Rails puts stuff

Here's a quick rundown of how to use these directories:

  • The controllers subdirectory is where Rails looks to find controller classes. A controller handles a web request from the user.

  • The views subdirectory holds the display templates to fill in with data from our application, convert to HTML, and return to the user's browser.

  • The models subdirectory holds the classes that model and wrap the data stored in our application's database. In most frameworks, this part of the application can grow pretty messy, tedious, verbose, and error-prone. Rails makes it dead simple!

  • The helpers subdirectory holds any helper classes that assist the model, view, and controller classes. This helps to keep the model, view, and controller code small, focused, and uncluttered.

Here's a typical configuration file (# characters comment out lines) for MySQL used with RoR is:

#MySQL (default setup). Versions 4.1 and 5.0 are recommended. # #Get the fast C bindings: # gem install mysql # (on OS X: gem install mysql -- --include=/usr/local/lib) #And be sure to use new-style password hashing: # http://dev.mysql.com/doc/refman/5.0/en/old-client.html development: adapter: mysql database: Energy username: root password: #socket: /path/to/your/mysql.sock #Connect on a TCP socket. If omitted, the adapter will connect on the #domain socket given by the socket instead. host: localhost port: 3306

Here are two other commonly used RoR commands you can use to automate the generation of application files. The first:

ruby script/generate model cookbook

generates the application's model objects for database table interaction (if the database table was named cookbook). The second:

ruby script/generate controller hacks

generates a controller object for the web application path named hacks.

Curt Hibbs

Категории