Skip to content

Instantly share code, notes, and snippets.

@lukelex
Created December 20, 2012 22:56
Show Gist options
  • Select an option

  • Save lukelex/4349341 to your computer and use it in GitHub Desktop.

Select an option

Save lukelex/4349341 to your computer and use it in GitHub Desktop.
CORs Rails integration
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :cor
private
def cor
headers["Access-Control-Allow-Origin"] = "js-app-origin.com"
headers["Access-Control-Allow-Methods"] = %w{GET POST PUT DELETE}.join(",")
headers["Access-Control-Allow-Headers"] = %w{Origin Accept Content-Type X-Requested-With X-CSRF-Token}.join(",")
head(:ok) if request.request_method == "OPTIONS"
end
end
YourApp::Application.routes.draw do
...
match '*all' => 'application#cor', :constraints => {:method => 'OPTIONS'}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment