Skip to content

Instantly share code, notes, and snippets.

@mcollie1
Created April 18, 2014 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcollie1/11067484 to your computer and use it in GitHub Desktop.
Save mcollie1/11067484 to your computer and use it in GitHub Desktop.
class ApiController < ActionController::Base
skip_before_action :verify_authenticity_token
before_filter :cors_preflight_check
after_filter :cors_set_access_control_headers
# necessary in all controllers that will respond with JSON
respond_to :json
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
headers['Access-Control-Allow-Headers'] = 'Content-Type'
headers['Access-Control-Max-Age'] = "1728000"
end
def cors_preflight_check
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version'
headers['Access-Control-Max-Age'] = '1728000'
end
private
# Error responses and before_filter blocking work differently with Javascript requests.
# Rather than using before_filters to authenticate actions, we suggest using
# "guard clauses" like `permission_denied_error unless condition`
def permission_denied_error
error(403, 'Permission Denied!')
end
def error(status, message = 'Something went wrong')
response = {
response_type: "ERROR",
message: message
}
render json: response.to_json, status: status
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment