Skip to content

Instantly share code, notes, and snippets.

@dasibre
Created June 4, 2020 13:51
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 dasibre/8eb218e74e148a5f61409b645321a63d to your computer and use it in GitHub Desktop.
Save dasibre/8eb218e74e148a5f61409b645321a63d to your computer and use it in GitHub Desktop.
Rails CORS setting
before_action :cors_preflight_check
after_action :cors_set_access_control_headers
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT'
headers['Access-Control-Max-Age'] = "1728000"
end
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment