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.
To develop a beginning Rails web application, install the following software:
Step 1: Installing Ruby
Installing Ruby couldn't be any simpler:
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):
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.
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:
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.
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:
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
Curt Hibbs |
Категории