Skip to content

Instantly share code, notes, and snippets.

@ryanlindsey
Created May 30, 2013 21:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ryanlindsey/5681375 to your computer and use it in GitHub Desktop.
Save ryanlindsey/5681375 to your computer and use it in GitHub Desktop.
An example Rails 3.2 controller that sets the CORS access control headers for cross-domain access
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :cors_preflight_check
after_filter :cors_set_access_control_headers
# For all responses in this controller, return the CORS access control headers.
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
headers['Access-Control-Max-Age'] = "1728000"
end
# If this is a preflight OPTIONS request, then short-circuit the
# request, return only the necessary headers and return an empty
# text/plain.
def cors_preflight_check
if request.method == :options
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'
render :text => '', :content_type => 'text/plain'
end
end
end
Copy link

ghost commented Jun 16, 2015

This is not working for the apps deployed in IBM Bluemix. Any idea how to enable CORS for the apps deployed in Bluemix cloud platform?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment