Agile Web Development

Build it. Launch it. Love it.

RESTful Rails

This plugin adds some advanced support for HTTP's features to regular rails controllers, like conditional GET/PUT/POST/DELETE, per-HTTP method dispatch handing, transparent OPTIONS support and several others.

Simple example

Here's a sample controller using per-method dispatching and conditional request handling:

class BookController < ApplicationController include RestController::Base

 resource :new do |r|
   conditions << @book = Book.new

   r.get(:cache_as => :public, :for => 1.hour)
 end

 resource :collection do |r|
   conditions << @books = Book.find(:all)

   r.post do
     @book = Book.new(params[:book])
     if @book.save
       render_post_success :action => 'by_id', :id => @book
     else
       render :action => 'new', :status => HTTP::Status::UNPROCESSABLE_ENTITY
     end
   end
 end

 resource :by_id do |r|
   conditions << @book = Book.find(params[:id])

   r.put do
     @book.attributes = params[:book]
     if @book.save
       render_put_success
     else
       render :status => HTTP::Status::UNPROCESSABLE_ENTITY
     end
   end

   r.delete do
     if @book.destroy
       render_delete_success :id => nil
     else
       render :status => HTTP::Status::UNPROCESSABLE_ENTITY
     end
   end
 end

end

Vitals

Home http://rubyforge.org/projects/restful-rails/
Repository svn://rubyforge.org/var/svn/restful-rails/trunk
License Rails' (MIT)
Tags Tag_red REST
Rating (5 votes)
Owner Dan Kubb
Created 24 April 2006

Comments

Add a comment