Browse the Ruby on Rails Community.

You are here: Browse Railsplugins Simply Restful

Simply Restful

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
end
def new
  # return an HTML form for describing a new message
end
def create
  # create a new message
end
def show
  # find and return a specific message
end
def edit
  # return an HTML form for editing a specific message
end
def update
  # find and update a specific message
end
def delete
  # delete a specific message
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")
end
  1. return an HTML form for defining a new message response = http.get(”/messages/new”)
  1. create a new message response = http.post(”/messages”, ”...”)
  1. retrieve message #1 response = http.get(”/messages/1”)
  1. return an HTML form for editing an existing message response = http.get(”/messages/1;edit”)
  1. update an existing message response = http.put(”/messages/1”, ”...”)
  1. delete an existing message response = http.delete(”/messages/1”)

The #resource method accepts various options, too, to customize the resulting routes:

map.resource :message, :path_prefix => ”/thread/:thread_id”
  1. —> GET /thread/7/messages/1
map.resource :message, :collection => { :rss => :get }
  1. —> GET /messages;rss (maps to the #rss action)
  2. also adds a url named “rss_messages”
map.resource :message, :member => { :mark => :post }
  1. —> POST /messages/1;mark (maps to the #mark action)
  2. also adds a url named “mark_message”
map.resource :message, :new => { :preview => :post }
  1. —> POST /messages/new;preview (maps to the #preview action)
  2. also adds a url named “preview_new_message”
map.resource :message, :new => { :new => :any, :preview => :post }
  1. —> POST /messages/new;preview (maps to the #preview action)
  2. also adds a url named “preview_new_message”
  3. —> /messages/new can be invoked via any request method
map.resource :message, :controller => “categories”, :path_prefix => ”/category/:category_id”, :name_prefix => “category_”
  1. —> GET /categories/7/messages/1
  2. has named route “category_message”

NOTE: This description has been extracted from the Plugin README and so the formatting may need updating to make browser friendly

Users


See all 8 member details


Membership

+ Join this railsplugin

Record Maintainer

'None'