Skip to content

Instantly share code, notes, and snippets.

@dacur
Created September 3, 2014 14:21
Show Gist options
  • Select an option

  • Save dacur/8c1f40618845edb94a91 to your computer and use it in GitHub Desktop.

Select an option

Save dacur/8c1f40618845edb94a91 to your computer and use it in GitHub Desktop.
Chrome and/or other browsers may not allow cross communication between localhost development environments (i.e. localhost:3000 => localhost:3001) due to browser security features. The code below represents the totality of the application_controller.rb file, which will then 'fix' this browser bug.
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery
before_filter :cors_preflight_check
after_filter :cors_set_access_control_headers
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, PUT, DELETE, GET, OPTIONS'
headers['Access-Control-Request-Method'] = '*'
headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept, Authorization'
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'] = '*'
headers['Access-Control-Max-Age'] = '1728000'
render :text => '', :content_type => 'text/plain'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment