Skip to content

Instantly share code, notes, and snippets.

@danielalvarenga
Created October 8, 2015 17:48
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 danielalvarenga/672661b3c41526601e51 to your computer and use it in GitHub Desktop.
Save danielalvarenga/672661b3c41526601e51 to your computer and use it in GitHub Desktop.
Add Header params to configure CORS for Rails 4 API
before_filter :allow_cors
def allow_cors
headers["Access-Control-Allow-Origin"] = "*"
headers["Access-Control-Allow-Methods"] = %w{GET POST PUT DELETE OPTIONS}.join(",")
headers["Access-Control-Allow-Headers"] = %w{Origin Accept Content-Type X-Requested-With X-Prototype-Version X-CSRF-Token token}.join(",")
headers['Access-Control-Max-Age'] = "1728000"
if request.method == 'OPTIONS'
render :text => '', :content_type => 'text/plain'
end
end
# permit options method
match "path/to/action" => "controller#action", via: [ :post, :options]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment