[uf-rest] Fwd: REST and Rails
David Heinemeier Hansson
david at loudthinking.com
Sun Apr 16 10:51:32 PDT 2006
Hi guys,
Just wanted to let you know that we've been very inspired by the
thoughts on this list and elsewhere about better RESTing in Rails.
We've just added verb support for Routing (http://dev.rubyonrails.org/
changeset/4209) and are currently exploring a very simple name-
convention based approach to dispatching verbs to controllers in the
Simply Restful plugin: http://svn.jamisbuck.org/rails-plugins/
simply_restful/
Here's the README for that plugin:
SimplyRestful
=============
SimplyRestful is a plugin for implementing verb-oriented controllers.
This is
useful for implementing REST API's, where a single resource has
different
behavior based on the verb (method) used to access it.
Giving credit where credit is due, this idea was inspired by reading:
http://pezra.barelyenough.org/blog/2006/03/another-rest-controller-
for-rails/
Because browsers don't yet support any verbs except GET and POST, you
can send
a parameter named "_method" and the plugin will use that as the
request method,
instead.
For example:
class MessagesController < ActionController::Base
def index
# return all messages
end
def create
# create a new message
end
def show
# find and return a specific message
end
def update
# find and update a specific message
end
def delete
# delete a specific message
end
end
Your routes would be something like:
map.resource :message
Then (using Net::HTTP to demonstrate the different verbs):
Net::HTTP.start("localhost", 3000) do |http|
# retrieve all messages
response = http.get("/messages")
# create a new message
response = http.post("/messages", "...")
# retrieve message #1
response = http.get("/message/1")
# update an existing message
response = http.put("/message/1", "...")
# delete an existing message
response = http.delete("/message/1")
end
--
David Heinemeier Hansson
http://www.37signals.com -- Basecamp, Backpack, Writeboard, Tada
http://www.rubyonrails.com -- Web-application framework
http://www.loudthinking.com -- Broadcasting Brain
More information about the microformats-rest
mailing list