Saturday, January 13, 2007

Introduction video to ActiveResources RESTful Rails controller actions

The world is talking about REST. Just when you are reading voraciously about Ruby and Rails, what would be nicer than having DHH to tell you what the next version of Rails' vision is regarding the whole REST idea?

Consider how you would write 4 named routes for the following ActiveController actions today:

POST /people/create
GET /people/show/1
POST /people/update/1
POST /people/destroy/1

ActionController::Routing::Routes.draw do |map|
map.with_options(:controller => 'people') do |people|
people.connect 'people/create', :action => 'create'
people.connect 'people/show/:id', :action => 'show'
people.connect 'people/update/:id', :action => 'update'
people.connect 'people/destroy/:id', :action => 'destroy'
end
end


How many times have you done CRUD on a model? With ActiveResources, all your need is 1 line of route setting:

  POST /people
GET /people/1
PUT /people/1
DELETE /people/1

ActionController::Routing::Routes.draw do |map|
map.resources :person
end


Convention over configuration can buy you a lot of coding. Rails does just that here (and more). Enjoy.

Presentation (pdf)
There is also a ActiveResources cheatsheet

No comments: