Putting REST on Rails
RESTful Dispatching in Rails
"Convention over configuration" is one of the key principles behind the design of Ruby on Rails. It was built with a specific way of doing things and as long as you follow the "Rails way" everything will just work with little or no configuration. That's part of the reason Rails is so successful--as long as you follow its conventions you get many benefits with less work than other frameworks that require you to be more explicit.
The HTTP protocol is very similar in this respect. Stray from the path and you'll find yourself hacking together replacements for basic things HTTP gives you for free. Stay on the path and you gain benefits like caching and better scalability of your applications with less effort. A well-designed--which typically means a RESTful--web application makes good use of available HTTP methods, rather than relying on
GET requests to trigger server state changes.
The ideal is to dispatch to a different handler based on the HTTP method, actually running different code depending on the method, and making it impossible to unintentionally change state with a
Chances are, most developers aren't thinking about HTTP simply because it isn't something that's been talked about by mainstream developers until recently. It's pretty common to be a developer and not know much about REST and HTTP, or know why it's important. But what if conventions were established in Rails that gradually steer developers onto a path that makes RESTful applications easier to develop?
With that in mind I set out to develop the RESTful Rails plugin that would make REST applications easier to create in Rails. The project was inspired by the article On HTTP Abuse that outlines features in HTTP that most frameworks don't completely support. My primary goal is to round out support for features that aren't already covered in Rails, while requiring only minimal changes to the way Rails applications are currently being developed.
What You'll Learn
This article introduces a very simple application that uses the RESTful Rails plugin. It will provide an introduction to dispatching to different handlers based on the HTTP method used in a request. If you plan on following along you should already have the latest version of Rails installed (1.1.1 at the time of writing), along with a database of your choice. I will also assume you have basic knowledge about how to configure your database for Rails using
config/database.yml. You should also have a Subversion client installed since we'll be using it to install the RESTful Rails plugin.
If you're not sure, here are a few excellent tutorials to get you started:
If you already have Rails installed you can upgrade to the latest version of Rails with the following command:
gem install rails --include-dependencies
You should have at least Rails 1.1.1 to follow along with this article, since some of the features we'll be using were not introduced until very recently.
Let's start off by describing the example we'll be using, a simple app to track a personal library of books. To start the Rails project, run the following from the command line:
rails --database=mysql library
Please note that the
--database=mysql switch tells Rails to preconfigure
config/database.yml for a MySQL database. Alternatively you can use any of the following options if you prefer:
We then move into the library directory that was just created for this project: