Plugins - Rest Controller
Add to favoritesRest Controller
Author: Brasten Sager Date: September 18th, 2006 License: MIT-LICENSE
Rest Controller provides basic default actions for a RESTful controller which can be overridden easily.
There is basic customization capabilities of the default responses. If you need more customization, you should just implement the method in question to bypass this plugin. If you find yourself overriding more than one or two of the default methods, then you probably should not be using this plugin to begin with.
Usage
# Controller with default actions.
class ProductsController < ApplicationController
rest_controller :product
end
Default Actions/Responses
These examples use @product / @products, but your actual variable name will depend on the model. "rest_controller :customer" will result in @customer / @customers, for example.
Index - find(:all) on your model.
HTML --> renders index.rhtml
XML --> renders @products.to_xml
RNG --> renders @products.to_rng (if simply_relaxed plugin is installed.)
Show - find(params[:id])
HTML --> renders show.rhtml
XML --> renders @product.to_xml if found / Status Code 404 if not.
Edit - find(params[:id])
HTML --> renders edit.rhtml
New - Model.new
HTML --> renders new.rhtml
Update - update(params[:id], params[:product])
HTML --> sets flash[:notice] appropriately
redirects to show(id) if successful / renders edit.rhtml if not
XML --> Status Code 204 if successful / Status Code 400 if not
Create - create(params[:product])
HTML --> sets flash[:notice] appropriately
redirects to show(id) if successful / renders new.rhtml if not
XML --> Status Code 201 if successful / Status Code 400 if not
Destroy - destroy(params[:id])
HTML --> sets flash[:notice] appropriately
redirects to index
XML --> Status Code 204 / Status Code 404
Overriding Responses
You can override the default responses by either providing a custom respond_to block, or provide a block for a specific action/mime-type.
# Overriding some responses.
class ProductsController < ApplicationController
rest_controller :product
# Override a DELETE/HTML response.
destroy_responds_to(:html) { render :text => "GONE!" }
# ... could also be formatted this way ...
destroy_responds_to :html do
render :text => "GONE!"
end
# Override all default responses for an action with a custom
# respond_to block.
show_responds_to do |wants|
wants.html
wants.xml { render :xml => @product.to_xml(... some includes ...) }
wants.graph { ... }
end
end
If you need to customize the logic for a particular action and cannot accomplish your desired result with before/meantime filters, you can simply implement the action in your class.
# Implement your own actions.
class ProductsController < ApplicationController
rest_controller :product
# Overrides the 'update' action.
def update
# Do something special for updating.
end
# You can trigger the default responses, if they still
# apply.
def index
# Some special code.
index_responder
end
end
Responders expect an instance variable by the name of the model. (Product == @product, etc). index expects the plural name (@products).
http://www.ibrasten.com/articles/2006/09/20/rest-controller-plugin
http://svn.superruby.com/svn/plugins/rest_controller
Rails' (MIT)
Controllers
