Skip to content

Instantly share code, notes, and snippets.

@sirwolfgang
Last active August 29, 2015 14:23
Show Gist options
  • Save sirwolfgang/ba990315eb6d2f3e1fea to your computer and use it in GitHub Desktop.
Save sirwolfgang/ba990315eb6d2f3e1fea to your computer and use it in GitHub Desktop.
API with Rails
config.middleware.insert_before 0, "Rack::Cors", :debug => true, :logger => (-> { Rails.logger }) do
allow do
origins '*'
resource '*',
headers: :any,
expose: 'X-Pagination',
methods: [:get, :post, :delete, :put, :patch, :options, :head],
max_age: 0
end
end
Jbuilder.key_format camelize: :lower
class BaseController < ApplicationController
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
rescue_from ActionController::BadRequest, with: :bad_request
skip_before_action :verify_authenticity_token
before_action do
params.deep_transform_keys!(&:underscore)
end
private
def bad_request(error)
render json: { error: error.message }, status: :bad_request
end
def record_not_found(error)
render json: { error: error.message }, status: :not_found
end
end
gem 'jbuilder'
gem 'rack-cors'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment