Skip to content

Instantly share code, notes, and snippets.

@camsong
Last active August 29, 2015 14:01
Show Gist options
  • Save camsong/0e162b83a103f21ad7b7 to your computer and use it in GitHub Desktop.
Save camsong/0e162b83a103f21ad7b7 to your computer and use it in GitHub Desktop.
CORS enabled in Rails 4
class ApplicationController < ActionController::Base
before_action :cors_set_access_control_headers
def options
head :ok
end
private
def default_serializer_options
{ root: false }
end
def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'PUT, POST, GET, OPTIONS'
headers['Access-Control-Request-Method'] = '*'
headers['Access-Control-Allow-Headers'] = 'Content-Type, X-Requested-With, X-Prototype-Version'
end
end
Rails.application.routes.draw do
match '*path', to: 'application#options', via: :options
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment