ONLamp.com
oreilly.comSafari Books Online.Conferences.

advertisement


What Is Ruby on Rails
Pages: 1, 2, 3, 4, 5, 6, 7

Rails Components

Rails itself consists of several components, which you can install and use separately. They are designed to work together seamlessly, though, and developers almost always use them together:



  • Active Record is the object-relational mapping (ORM) layer that connects business objects (models) to database tables. It is an implementation of the Active Record pattern described by Martin Fowler.
  • Action Pack is the component that implements both the view and controller portions of the MVC architecture. The controller part handles incoming requests from the user's browser and routes them to the correct method of a controller class. The view part builds the response to send back to the browser using a templating system similar to that of ASP or JSP.
  • Prototype is the component that implements the Ajax, drag-and-drop, and visual effects within your web pages.
  • Action Mailer is the component that handles the sending and receiving of email.
  • Action Web Service is the component that makes it easy to add web service APIs to your web application. Action Web Service supports SOAP, XML-RPC, and WSDL.

General Features

Rails has some general and some specific characteristics.

Web servers

Rails can run on just about any web server that implements CGI. However, the performance of CGI is notoriously bad, so the preferred deployment of a Rails application is to use FastCGI. There have been extensive tests of Rails application deployments with both Apache and LightTPD. There is also a newcomer, SCGI, which rivals the performance of FastCGI without the complicated setup.

During development, it is usually easiest just to use the WEBrick web server that comes built-in to Ruby.

Databases

Rails currently contains support for the following databases:

  • MySQL
  • PostgreSQL
  • SQLite
  • SQL Server
  • DB2
  • Oracle

It takes approximately 100 lines of Ruby code to implement a database adapter, so adding to this list is not particularly onerous.

Debugging

When something goes wrong inside your Rails web app, you normally get a pretty detailed error display in your browser (when running in development mode). Often this is enough to diagnose the problem. When it's not, you have other options:

  • Insert debugging output in your controller. For example:

    render_text "Got here"

    or

    render_text "user object = " + user_obj
  • Check the Rails log files. (Use tail on *nix systems.) Look for the files development.log, production.log, and fastcgi.crash.log. Remember that your web server has log files, too.
  • Use breakpoints.
  • Use a commercial IDE (like ArachnoRuby) with a built-in debugger.

Custom (pretty) URLs

The default Rails mapping of URLs to controller actions is very simple and easy to understand. Rails tries very hard to present the user with pretty URLs. Rails URLs are simple and straightforward, not long and cryptic.

Even so, you can still customize your URLs by using the Rails routing facility. Rails' URL routing is flexible enough to allow you to create virtually any URL mapping scheme.

The Rails routing facility is pure Ruby code that even allows you to use regular expressions. Because Rails does not use the web server's URL mapping (like mod_rewrite in Apache), your custom URL mapping will work the same on every web server.

Unit testing

Rails actively facilitates unit testing:

  • Generating new controllers, models, and scaffolding also creates corresponding unit test skeletons.
  • The strict MVC architecture tends to naturally result in testable actions and components.
  • Rails includes a Rake (Ruby Make) script that will automatically run all your unit tests.

You can find more details in A Guide to Testing Rails.

Active Record

Active Record is the part of Rails that handles the automatic mapping of your database tables to your runtime model objects. It's the M in MVC, and it is Rails' implementation of an ORM layer.

For all the common uses (and some of the not-so-common ones), you'll never need to see or use SQL when accessing or updating your database. Active Record's goal is specifically to work with relational databases; it does not try to abstract away its SQL usage. Active Record makes it easy to use your own custom SQL for those complicated cases where it is necessary. Even so, it is rarely needed.

Pages: 1, 2, 3, 4, 5, 6, 7

Next Pagearrow





Sponsored by: