animekun
animekun

Reputation: 1869

Can't install rake-cors in rails-api app

I'm pretty new to rails development, so I guess it is pretty easy issue, but I still can't find a solution for it. So I made an rails app, which works fine, except of CORS, I can't install rake-cors properly. I've added 'gem 'rack-cors', :require => 'rack/cors' to Gemfile, but after bundle-install I can't find cors.rb in config/initializers.

Did anyone crash into this, how can I solve it?

Upvotes: 0

Views: 454

Answers (2)

Trung H. Tran
Trung H. Tran

Reputation: 59

Add gemfile gem 'rack-cors'

Run bundle install

Add cors.rb file into config/initialize/cors.rb

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*', headers: :any, methods: [:get, :post, :options]
  end
end

Don't forget to restart your rails server

For more detail settings please read: http://www.rubydoc.info/gems/rack-cors/0.2.9

Upvotes: 1

adamliesko
adamliesko

Reputation: 1915

I don't think rack-cors would create a initializer. If you followed the README on Github, you have to do it yourself. Put this snippet of code into your application.rb file.

module YourApp
  class Application < Rails::Application

    # ...

    config.middleware.insert_before 0, "Rack::Cors" do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :options]
      end
    end

  end
end

There is not an initializer in the example app as well.

Upvotes: 1

Related Questions