Skip to content

Instantly share code, notes, and snippets.

@dcparker
Created February 10, 2010 20:24
Show Gist options
  • Save dcparker/300797 to your computer and use it in GitHub Desktop.
Save dcparker/300797 to your computer and use it in GitHub Desktop.
A versionable API routing-controller-action strategy.
# Routing
map.with_options(:namespace => 'api/', :path_prefix => "/api/:version", :version => /v\d+/) do |api|
api.resources :photos
end
# API Controller superclass
class ApiController < ApplicationController
append_view_path 'views/api'
# this will happen before anything else that cares about
# what params[:action] is, even before_filters.
def perform_action
params[:action] = @action_name = "#{params[:action]}_#{params[:version]}"
super
end
end
# Sample Api controller
class API::PhotosController < ApiController
def index_v1
@photos = Photo.all
end
def index_v2
@photos = current_person.photos
end
def show_v1
@photo = Photo.find(params[:id])
end
def show_v2
@photo = current_person.photos.find(params[:id])
end
end
# API views directory structure:
views/
api/
photos/
index_v1.json.erb
index_v2.json.erb
show_v1.json.erb
videos/
index_v1.json.erb
show_v1.json.erb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment