Created
April 18, 2014 22:45
-
-
Save mcollie1/11067484 to your computer and use it in GitHub Desktop.
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 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