Skip to content

Instantly share code, notes, and snippets.

@joefiorini
Created September 26, 2008 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joefiorini/13144 to your computer and use it in GitHub Desktop.
Save joefiorini/13144 to your computer and use it in GitHub Desktop.
Code to make resource controllers more RESTful
class ApplicationResourceController < ApplicationController
resource_controller
protect_from_forgery :only => [:update, :delete]
index.wants.xml { render :xml => collection.to_xml, :status => :ok }
show do
failure.wants.xml { render :nothing => true, :status => 404 }
wants.xml do
unless object
render :nothing => true, :status => 404
else
render :xml => object.to_xml, :status => :ok
end
end
end
update do
failure.wants.xml do
render :nothing => true, :status => :conflict if object.conflicting_name? params[:id]
end
wants.xml { render :xml => object.to_xml, :status => :ok }
end
create do
wants.html { }
wants.xml { render :xml => object.to_xml, :status => :created, :location => object_url(object) }
failure.wants.xml { render :nothing => true, :status => :conflict if object.conflicting_name? params[:id] }
end
destroy do
wants.xml { render :nothing => true, :status => :ok }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment