How to enable CORS in Rails 4 App
Rack::Cors provides support for Cross-Origin Resource Sharing
Steps to enable rackcors :
Add this gem to your Gemfile:
gem 'rack-cors'
Add the code below to config/application.rb
If you are using Rails 3/4:
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', :headers => :any, :methods => :any
end
end
If you are using Rails 5:
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: :any
end
end
You should use rack cors
It provides a nice DSL, to use in your config/application.rb
, instead of the messy header work and before filters.
A very permissive would be as follows, but of course, you'll have to tailor it a bit.
use Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: :any
end
end