Skip to content

Instantly share code, notes, and snippets.

@cjse
Created July 23, 2008 21:46
Show Gist options
  • Save cjse/1910 to your computer and use it in GitHub Desktop.
Save cjse/1910 to your computer and use it in GitHub Desktop.
REST/CRUD conventions from http://project.ioni.st/post/2283#snippet_2283
class AccountsController < ApplicationController
set_resource :account
def index
@accounts = Account.all
default_formats_for @accounts
end
def show
default_formats_for @account
end
end
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