Can't get rack-cors working in rails application

后端 未结 6 1236
野的像风
野的像风 2020-12-12 19:47

I wanted to implement CORS in my rails application, so I googled rack-cors gem for it. And I did everything as was said in README, that is updated Gemfile accordingly and up

相关标签:
6条回答
  • 2020-12-12 20:03

    After all it came out that this gem has some issues with heroku, on the local machine it works perfectly fine.

    0 讨论(0)
  • 2020-12-12 20:07

    I had to create a special route to handle the options requests, the cors gem didn't do it for me like I expected it to. The route I added to the end of routes.rb was:

      match "*path", :to => proc {|env| [200, {
      'Access-Control-Allow-Origin' => '*',
      'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS',
      'Access-Control-Allow-Credentials' => 'true',
      'Access-Control-Request-Method' => '*',
      'Access-Control-Allow-Headers' => 'Origin, X-Requested-With, Content-Type, Accept, Authorization',
    'Content-Type' => 'text/plain'
    
     }, ["CORS Preflight"]] }, :via => [:options]
    
    0 讨论(0)
  • 2020-12-12 20:11

    Make sure you added or uncommented gem 'rack-cors' in the Gemfile

    0 讨论(0)
  • 2020-12-12 20:12

    There is a new issue thread for the heroku solution

    Instead of using

    config.middleware.use Rack::Cors do
    

    try

    config.middleware.insert_before ActionDispatch::Static, Rack::Cors do
    

    That worked for me.

    0 讨论(0)
  • 2020-12-12 20:21

    I ran into the same problem with heroku. I found this blog with the same rack-cors issue.

    Just moved the use Rack::Cors to config.ru, redeployed to heroku and it works.

    require ::File.expand_path('../config/environment',  __FILE__)
    run Rails.application
    
    require 'rack/cors'
    use Rack::Cors do
    
      # allow all origins in development
      allow do
        origins '*'
        resource '*', 
            :headers => :any, 
            :methods => [:get, :post, :delete, :put, :options]
      end
    end
    
    0 讨论(0)
  • 2020-12-12 20:24

    Here's how I fixed mine:

    You just need to un-comment the Rack CORS gem in your Gemfile (if it's there) or just add it:

    gem 'rack-cors'
    

    And then run the code below to install the gem:

    bundle install
    

    Put the code below in config/application.rb of your Rails application. For example, this will allow GET, POST or OPTIONS requests from any origin on any resource:

    module YourApp
      class Application < Rails::Application
        # ...
    
        # For Rails 5 Appications
    
        config.middleware.insert_before 0, Rack::Cors do
          allow do
            origins '*'
            resource '*', headers: :any, methods: [:get, :post, :options]
          end
        end
    
        # For Rails 3/4 Applications
    
        config.middleware.insert_before 0, "Rack::Cors" do
          allow do
            origins '*'
            resource '*', headers: :any, methods: [:get, :post, :options]
          end
        end
      end
    end
    

    Setting origins to '*' should be alright for development, but keep in mind that if you deploy to production you’ll want to change this value to match your front-end’s URI for security reasons.

    Note: If you're running Rails, updating in config/application.rb should be enough. There is no need to update config.ru as well.

    That's all

    I hope this helps.

    0 讨论(0)
提交回复
热议问题