Just Talk About Web

Techniques Behind Modern Web

Ruby on Rails




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.)

aptana_install_rails.gif

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“.

new_rails_project.gif

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

project_skeleton.gif

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.

scaffold_create.gif

See “Console” view for what have been created.

scaffold_create_console.gif

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.

databaseyml.gif

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

create_bookrb.gif

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

rake_db_migrate.gif

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.

start_server.gif

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

empty_books_view.gif

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

new_book_view.gif

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

book_created.gif

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

listing_book_view.gif

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.

restful-test-get-book-list.gif

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

        Andy Oram, Greg Wilson
        2008-01-24T17:35:47+07:00
        1
38.69
 
        2008-01-24T17:35:47+07:00
 
        Mark Lutz, David Ascher, Frank Willison
        2008-01-24T17:40:22+07:00
        2
24.0
 
        2008-01-24T17:40:22+07:00

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
30.49

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.

restful-test-add-book1.gif

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

restful-test-add-book2.gif

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

restful-test-add-book31.gif

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:

31.49

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

http://localhost:3000/books/4.xml

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

restful-test-update-book1.gif

Now, you can see the update via browser.

restful-test-update-book2.gif

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:

http://localhost:3000/books/2.xml

restful-test-delete-book1.gif

Learning Python” is gone:

restful-test-delete-book2.gif

Conclusion

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!

Looking for Prototype/script.aculo.us replacement on Rails? jRails project can be an answer if you prefer JQuery. Here is info from project site:

Features

jRails provides drop-in functionality for these existing Rails methods.

Prototype

  • form_remote_for
  • form_remote_tag
  • link_to_remote
  • observe_field
  • observe_form
  • periodically_call_remote
  • remote_form_for
  • submit_to_remote

Scriptaculous

  • draggable_element
  • drop_receiving_element
  • sortable_element
  • visual_effect

RJS

  • hide
  • insert_html
  • remove
  • replace
  • replace_html
  • show
  • toggle

Visual Effects

The visual effects in jRails are based on… Read more »

Now Come “The Rails Way”

Long awaited book stating itself as the “expert guide to building Ruby on Rails applications” has finally come.

This book will help you:

  • Increase your productivity as a web developer
  • Realize the overall joy of programming with Ruby on Rails
  • Learn what’s new in Rails 2.0
  • Drive design and protect long-term maintainability with TestUnit and RSpec
  • Understand and manage complex program flow in Rails controllers
  • Leverage Rails’

Read more »

Myles Eftos has given 6 things try Rails this year. They are:

  1. Install Rails: This is aimed at those of you out there that haven’t tried Ruby on Rails yet. Jump in – have a go, there are plenty of resources out there, and it is fairly easy to install regardless of your platform
  2. Upgrade to Rails 2.0: I have covered what’s new in Rails 2.0 in a number of my previous posts, and upgrading

Read more »

  • 0 Comments
  • Filed under: Ruby on Rails
  • Do you need GWT on Rails?

    Oh, my first thought was “It’s silly to add Java to Rails” because normally GWT uses the Java language to generate JavaScript. But I changed my mind after reading Jon Crosby‘s answers to a short interview from InfoQ:

    Most of Java’s weight comes from JEE, which is not part of GWT on Rails. Client side GWT uses the Java language to generate JavaScript; The heavy server side platform never makes it into the picture.

    Read more »

  • 0 Comments
  • Filed under: GWT, Ruby on Rails
  • David H. HanssonPhoto: 43people.com

    InfoQ had an interesting interview with David H. Hansson, the creator of Ruby on Rails and its biggest proponent. He revealed many improvements in Rails 2.0 and how to upgrade from previous version. Here are some points:

    • Rails 2.0 is polished in a way that’s very hard to describe meaningfully in words, but very apparent when you use it with hundreds of tiny changes.
    • Migrating from Rails 1.2.x

    Read more »

  • 2 Comments
  • Filed under: Ruby on Rails
  • Premium Sponsors

    Website Magazine

    Career Opportunities

    Advertisements