- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
When Jen Bortel, my boss here at Informit, first asked me to do some content on Ruby, I was thrilled. After all, I've been hearing just as much about Ruby, and Ruby on Rails, as you have. Maybe even more. I kept hearing a lot of talk about how easy it was, how productive you can be, how fast it is to develop in, and all of those things we programmers love to hear.
After stumbling around for a few days trying to get everything configured, I was beginning to wonder. Fortunately, the problem was me. If you read the right documentation, getting an application running in Ruby -- and particularly in Ruby on Rails -- is almost ridiculously easy. Before you finish this guide entry, you're going to see just how ridiculously easy it is.
This entry begins a series on developing with Ruby and with Ruby on Rails. In it, we're going to develop a simple survey application. We'll start with a commandline interface, and then move to the Web.
But first we have to answer these simple questions: what the heck is Ruby anyway? And why we're at it, what is Rails? And why is Ruby on it?
Let's start at the beginning.
Simply put, Ruby is an object-oriented language. It has its own syntax, which may or may not seem familiar to you based on what languages you're familiar with. It is designed to make programming easy. And in many ways, it does just that, though I'm a little skeptical of Ruby's claims that its syntax is much like speech. You can build all sorts of applications using Ruby. You can build commandline applications, window based applications, and even Web applications. You can run Ruby as a CGI application. You can use Ruby in an environment in which it acts much like PHP, with embedded commands on the page.
And you can and do all of that without Rails.
So why do you even need Rails? What is the big deal about it? What is it?
Rails, in my opinion, is the reason you're hearing so much about Ruby. Rails is an application framework that enables you to create Ruby applications for the Web in no time. Well, in almost no time.
It happened like this: David Heinemeier Hansson was working on a web application called Basecamp. Basecamp is a project management application, but the important thing is that Hansson decided that there were certain things that he had to do over and over again that had nothing to do with the actual application. For example, accessing the database. Or finding a particular object. Or creating a Web form where people could enter information.
So he did what many of us have done in the same situation; he created a framework that did a lot of the "heavy lifting" for us. The difference is that this framework is for Ruby applications, and it's now publicly available. That framework became Rails.
To sum up so far, Ruby is a new programming language, and Rails is a particular kind of application written in Ruby.
For the rest of this entry, I'm going to show you how easy it can be to get a Rails application up and running. I'll be giving specific instructions for Windows, but the Linux instructions are basically the same. (Also, I'll be ducking the whole web server issue by using the development HTTP server that comes with Rails.)
The basic process looks like this:
- Install Ruby.
- Install Rails.
- Create the database tables.
- Have Rails create the skeleton web application.
- Start the web server
- Configure the web application.
- Add the code.
Installation
Let's start with installation:
- First, you'll need to install Ruby, and Rails. Download Ruby for Windows or other platforms. (Check the Ruby Home Page for the most recent version.)
- If you're installing on Windows, and you've downloaded the one click installer, run the installer to complete the installation. If you're installing from source, follow the directions with your distribution.
- Download RubyGems. This is the Ruby package manager. Unpack it into your Ruby directory.
- Install Rails by executing the following command from the commandline:
gem install rails
You will need to be online for this to work. Accept all of the dependencies.
Create the database
Now that you've got everything installed, let's talk about the application we're going to ultimately build. It's a simple survey application, with the ability to add new polls, each of which has one or more questions, each of which has one or more answers. We'll look at multiple ways to store this information, but for right now, we're going to use a database.
Ruby works with multiple databases, but for our purposes, we're going to use MySQL. (If you haven't already got it installed, download it from http://www.mysql.com/.) Log into the interactive console and create the following objects:
create database surveysays; create table surveys ( `id` INT NOT NULL AUTO_INCREMENT , `title` VARCHAR( 100 ) NOT NULL , PRIMARY KEY ( `id` ) );
Ultimately, we'll have tables for the questions, answers, and results,
but for now, we'll use only the surveys
table to keep things simple.
Create the web application
It is Rails that creates the actual web application, so open a command prompt window and change to the directory in which you want your web application to live. For example, I want my full path to be:
c:\Inetpub\Surveysays
so I change to the c:\Inetpub
directory and type:
rails Surveysays
In just a moment or so, Rails creates the entire structure of the application.
Start the web server
To make things simple, Rails comes with a simple web server called Webbrick. You wouldn't want to run your production application with it, but depending on your platform, setting up a production web server for Rails can be a significant challenge.
To start the server, change to the Surveysays
directory and type:
ruby script\server
To make sure it's working, open your browser and point it to:
http://localhost:3000
You should see a page something like this.
Configure the application
Rails will handle all of the database interaction for you, but you have to tell it
where to find the database. To do that, use a text editor to edit the config\database.yml
file so that the first section reads:
development: adapter: mysql database: surveysays host: localhost username: root password:
Make sure, of course, that the information is accurate for your installation.
Add the code
Even though we haven't added a single line of code yet, we are actually almost done. We're going to create a simple web application that lists all of the available surveys, and includes the ability to add, edit, and delete individual surveys.
Later, when we go into building a Rails application in detail, we'll talk about the Model-View-Controller pattern it uses as its architecture. For now, though, understand that we are linking the logic portion (the Controller) with the data portion (the Model).
Start by creating the Model and Controller by typing the following at the
commandline from the Surveysays
directory:
ruby script\generate model Survey ruby script\generate controller survey
Using your text editor, open the Surveysays\app\controllers\survey_controller.rb
file. Add the following:
class SurveyController < ApplicationController model :survey scaffold :survey end
All we've done here is let the controller know what kind of object it is
responsible for. The second line, the scaffold
, provides
everything we need for maintaining this data through the web. Point your browser
at:
http://localhost:3000/survey/list
You should see something like:
We don't have any records yet, so of course nothing is displayed.
But you can use the New Survey
link to create a new one,
and the subsequent links to edit or delete them. By just adding these two lines of code,
we've created an application -- albeit a very simple one -- that enables us
to manage this data.
Of course, this is not an application that we would ultimately release, and Ruby on Rails enables us to customize any aspect of the display or logic. This is just an example of how simple the process really is.
Next week, we'll talk about the actual application, and look at the basics of the Ruby language itself.