First Steps in Rails, RESTful by Example, Part 1: Beast

I’m migrating one of my main e-commerce sites from php to Rails. Coming from a few years of Java and php makes me really appreciate this framework’s rapid development cycles, as well as strict MVC approach.

Note: this is a technical post, it assumes you’re interested in, and familiar with Rails.

Rails wants to be RESTful. Many sites and books go into depth explaining REST. Last year’s Railsconf keynote by DHH can give you the big picture of CRUD and RESTful applications. (Be sure to follow along with the pdf slides, while you watch the video).

Look at an existing mature project (Beast) to learn how to make RESTful models

Nitty gritty RESTful implementation details have been hard to find. So I started searching out open source projects that are known for good design. And I read the code.

Beast is a forum program written in Rails by Rick Olson and Josh Goebel. Best of all, it’s a great example of a REST application.

I’m a visual person, so I created the chart below to help me get the hang of what the Models and relationships look like in Beast. Big bold squares are actual database backed objects. Dotted squares are just regular models.

Beast Restful Model Diagrams
Please contact me if you catch an error or omission.

Here’s how you use the diagram

Right now, browse here: http://svn.techno-weenie.net/projects/beast/trunk/app/models to the Subversion source of Beast. Back already? My you’re fast. Read over the different model objects. See how they relate. Think about what’s been abstracted into additional Resources, rather than creating actions willy nilly in the controllers.

Then check it out and run it in your local environment. Even if you couldn’t care less about the (quite nice) forum software that it is, it’s good to learn by taking it for a spin.

But I think I need more actions than CRUD gives me!

As DHH said in the 2006 Railsconf keynote, sometimes you think you need another non-CRUD verb to your controller. Say you have a Forum that has Users and Topics. Some users want to monitor topics. At first blush, you may want to add an action to Topic. You’d then post to /topic/monitor/123 to start monitoring topic 123.

There is another way.

Relationships, events and states can all be models. When a new topic is to be monitored, you’ll GET to /monitorships/new to get the new Monitorship object form. Then POST to /monitorships to actually create it.

Models are more than things.

Bookmark with your social bookmarking tool of choice:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • digg
  • Reddit
  • Spurl
  • YahooMyWeb
  • co.mments
  • Smarking

6 Responses to “First Steps in Rails, RESTful by Example, Part 1: Beast”

  1. moxie Flex RIA Says:

    Thanks for dug all these stuffs out. It could be very useful for who ever want to experience the beauty of Ruby on Rails. The Beast mode is the first thing a RoR developer should have a good understanding.

  2. Sonal Says:

    This figure is very useful, especially for those new to RESTful concepts. And yes, it is great temptation to add more actions to the REST controller!

  3. Brian Says:

    I don’t see adding more models and increasing database complexity as the right solution to avoid adding actions… Its like trying to speak English but you only get 7 words… I understand that REST is all the rage, and its cute and easy for some things, but as an application… I’m not seeing it… Applications have business logic that needs to really do more than just save and load data.. For that heavier lifting, I think I still to web services.. At some point you have to code more than just a blog or a forum.

  4. Tom Says:

    Hi,

    Were you able to develop a RESTful ecommerce website? I am not sure that rest is the best way to create cart and checkout management.

  5. Andy Says:

    I looked at the beast source - it is a train wreck!!! Just awful…

  6. 3hv » Blog Archive » Ruby on Rails Basics Says:

    […] Use RESTful designs in your controllers.  This means that your controller very rarely grows beyond seven actions (index, show, new, create, edit, show, destroy) - and RESTful routing ensures that GETs and POSTs go to the right places.  If you need to extend things then add a sub-controller.  For example, if you were transferring an employee to another, you could have a route: /employees/1 for the employee him/herself, /employees/1/transfers/new to let the user define which company the employee is moving to and /employees/1/transfers/create to actually do the transfer.  And if you’ve defined your model correctly, the implementation of /employees/1/transfers/create should be as simple as @employee.transfer_to(@company) […]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>