Rails 2.0 reveals many changes and improvements, but the biggest change involves getting closer to the full web-orientation of REST and HTTP. With a few lines of codes now you can easily create open web applications based on modern web service.

Creating Rails application

This example is a deadly-simple bookstore CMS. All you can do are to get book list, add or delete books. Not much? Yes, but big news is you can do that from anywhere in the world with or without a browser.

First, I use RadRails, which is now a part of Aptana Studio, to create a Rails 2.0 application (you can learn more about how to add RadRails to Aptana Studio from this page. It should be matter of one click only.)


Please note that you have to upgrade to Rails 2.0, if not yet, here are links to the Ruby Windows installer, Mac OS 10.4 instructions.

Just follow step-by-step guide to create a Rails app below.

1. Switch to RadRails perspective, select File -> New -> Rails Project on main menu. Enter “bookstore” for project name and check “Create a Mongrel server“.


2. Click “Finish” button and the skeleton of Rails app is generated (see Console view for process details).


3. Open “Generators” view, select “scaffold” from the command list box (in previous Rails version, you have to use “scaffold_resource” command instead), then type in:

book title:string author:string price:decimal

as arguments. Click “Go” button — this will create a new scaffold for a model called ‘book‘ which has three fields, book title and book author typed as a text string, and a price of decimal type.


See “Console” view for what have been created.


Now, believe it or not, the bookstore application is almost completed, the only thing we need to do it to update the database so that it has the schema for the book records.

4. Change database names defined in “database.yml” file under “config” folder to more meaningful names.


5. Navigate to “db/migrate” folder and open file “xxx_create_books.rb“. You’ll see how “books” table is defined.


6. Switch to “Rake Tasks” view, select “db:migrate” from command list and click “Go” button.


7. Wow, that is! The bookstore application is ready to run now. Switch to “Server” view, “bookstoreServer” should be there with default configuration listening at port 3000. Just select it and click “Start” button to run.


8. Open your favorite browser, type http://localhost:3000/books/ in address bar. The book list is waiting for your first entry.


9. Click “New book” link and input a book title, author and price for it.


Then select “Create” button. Now, the book is created and stored to database as desired.


10. Enter info for other books and back to book list view.


Up till now, anyone who is familiar to Rails may find nothing really new in this tutorial. However, interesting part is in the next section which makes use of RESTful web services offered freely along the way.

Using RESTful web services

Well, to test how RESTful Web APIs are supported by Rails 2.0 we’ll need a client tool. For those who want to use command line only, cURL is highly recommended (see Duane Johnson’s tutorial for how to use it in testing.) However, I want all processes are visual, so I use Rest-Client — a Java desktop tool made for testing RESTful web services. Now, just start Rest-Client by issuing command (you must have Java 5 or above):

java -jar {path-to-rest-client-jar-file}

The Rest-Client tool should start and be ready for using in our tests.

1. Get book list

Type http://localhost:3000/books.xml to URL textbox, select “GET” as request method, and click “Request” button.


See XML version of book list returned in “Response Body” tab.

        Andy Oram, Greg Wilson
        Mark Lutz, David Ascher, Frank Willison

2. Add a new book

To add a new book, we have to prepare a XML text containing appropriate info about the book. Something should look like this for “The Rails Way”:

    Obie Fernandez

On Rest-Client tool, select “POST” request method (instead of “GET” in previous step), activate “Body” tab. Set request “Content-Type” to “application/xml” and paste XML into request body text region.


Click “Request” button. A new book created as returned XML in “Response Body” region shown.


And you can see new book added to the book list in browser too.


3. Update book info

Let say we want to update price of newly-added book “The Rail Way” to $31.49, prepare another XML with updated information:


Select “PUT” request method, paste the above XML to request body text region and change URL to:


where 4 is the ID of new book as you may notice. Click “Request” button to send it to Rails web API.


Now, you can see the update via browser.


4. Delete a book

Our last mission is to delete a book using web service. It’s quite a simple task, just select “DELETE” request method and send to book XML URL, for example:



Learning Python” is gone:



Though this appears a long tutorial but actually it would take less than 10 minutes to create and test the application/web services. I’m quite sure that you can develop the same capability in Java or .Net environments but it would not be that simple. Alert me if this inspires you to create the next Amazon.com (2.0) :) .

Download PDF of this tutorial.

Free Website Magazine: Know more than your competitors with Website Magazine

Don't forget to subscribe so you don't miss out on future posts!

Related Posts