Last active
August 29, 2015 14:23
-
-
Save sirwolfgang/ba990315eb6d2f3e1fea to your computer and use it in GitHub Desktop.
API with Rails
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
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 |
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 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 |
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
gem 'jbuilder' | |
gem 'rack-cors' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment