Created
July 23, 2008 21:46
-
-
Save cjse/1910 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REST/CRUD conventions from http://project.ioni.st/post/2283#snippet_2283 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AccountsController < ApplicationController | |
set_resource :account | |
def index | |
@accounts = Account.all | |
default_formats_for @accounts | |
end | |
def show | |
default_formats_for @account | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
def self.set_resource(resource, &custom_setter) | |
filter_name = :"set_#{resource}" | |
before_filter filter_name, | |
:only => [:show, :edit, :update, :destroy] | |
if custom_setter | |
define_method(filter_name) do | |
custom_setter.call(resource) | |
end | |
else | |
define_method(filter_name) do | |
instance_variable_set( | |
"@#{resource}", | |
resource.to_s.classify.find(params[:id]) | |
) | |
end | |
end | |
end | |
def self.default_formats_for(data) | |
respond_to do |format| | |
format.html | |
format.xml { render :xml => data } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment