Online Book Reader

Home Category

Beyond Java - Bruce Tate [67]

By Root 702 0
Rails can handle sophisticated domains with inheritance and relationships. And Rails is often enough to get the job done.

Keep an open mind. Judge for yourself.

* * *

[*] Justin Gehtland, Weblogs for Relevance, LLC (April 2005); http://www.relevancellc.com/blogs. I *heart* rails; Some Numbers at Last.

Rails by Example


The best way to understand Rails is to see it in action. Go to http://rubyforge.org and download Ruby and RubyGems . (If you use the Windows one-click installer, you'll get RubyGems with that distribution.) If you don't already have one, download a relational database manager, too. I used MySQL. You'll begin to get the Rails experience at install time. RubyGems lets you install Ruby applications and their dependencies. At the command line, type:

gem install rails -v 0.12.1

Ruby will start the installation process. It goes up to RubyForge (rubyforge.org) and pulls down an index including the appropriate version of Rails and its dependencies. If you were to omit the version number, Ruby would get you the latest stable version. RubyGems will then prompt you for each dependency. Answer "Y," or answer "a" once for all dependencies:

Attempting remote installation of 'rails'

Updating Gem source index for: http://gems.rubyforge.org

Install required dependency rake? [Yn] Y

Install required dependency activesupport? [Yn] Y

Install required dependency activerecord? [Yn] Y

Install required dependency actionpack? [Yn] Y

Install required dependency actionmailer? [Yn] Y

Install required dependency actionwebservice? [Yn] Y

Successfully installed rails, version 0.12.1

You'll notice that RubyGems will then attempt to build the documentation for each of the subcomponents and Rails. And that's it. Rails is installed. You're already getting hints about the approachability of Rails.

Generating a Basic Application


You can now generate a Rails project. Go to your working directory and ask Rails to generate a project called trails:

rails trails

Ruby creates a full directory structure that will contain your application. There's no guesswork, and all Rails projects will have a consistent format. I'll point out a few important directories:

app

This directory has your application code. You'll see a directory for each component of MVC and a couple of others.

config

This directory will be very light. You'll put in anything that needs special configuration, like the connection parameters for your database. Since Ruby makes excellent use of defaults, your config directory will stay sparse.

script

Your trails app comes with scripts that will help you generate code, and start your application server.

You'll notice a few other goodies as well, but for now, let's use one of the scripts to start Ruby's application server. Change to the trails directory, and type:

ruby script/server

If things are working, you'll see a server started on port 3000. You can go to http://127.0.0.1:3000/ to make sure things are running. You'll get a Rails welcome message. You just started a development Ruby web server, configured for Rails. If you need to change some properties of the server, you'll just change the script/server script. Notice that Ruby programmers typically do configuration, like this server script, in Ruby scripts. You've already learned that Ruby handles structured data well, without XML. For example, this is the part of the server script that has the configuration options:

OPTIONS = {

:port => 3000,

:ip => "0.0.0.0",

:environment => "development",

:server_root => File.expand_path(File.dirname(_ _FILE_ _) + "/../public/"),

:server_type => WEBrick::SimpleServer

}

This code simply defines a hash map called OPTIONS. The => operator maps keys on the lefthand side to values on the right. Nothing has really happened yet, but you should be paying attention. You've set up a whole lot of infrastructure in a very short time.

Our trails project will collect descriptions of mountain bike trails. We'll start simple, collecting an ID to go with a trail name, description, and difficulty.

Return Main Page Previous Page Next Page

®Online Book Reader