Monday, 27 January 2014

Fast Rails API with ActionController::Metal

#app/controllers/api/api_controller.rb
class Api::ApplicationController < ActionController::Metal
include ActionController::Rendering
respond_to :json
end
#app/controllers/api/v1/tasks_controller.rb
module Api
module V1
class TasksController < Api::ApplicationController
def do_something
render :json => Task.find(params[:id]).to_json
end
end
end
end
#routes
namespace :api , defaults: {format: 'json'} do
namespace :v1 do
resources :tasks, :only => [] do
member do
get 'do_something'
end
end
end
end
view raw gistfile1.rb hosted with ❤ by GitHub

2 comments:

  1. I was trying to figure out how to code an API using ActionController::Metal and yours was the first page out of dozens I tried with something that actually works. Thanks!

    ReplyDelete