rack-cors: uninitialized constant Rack::Cors

I’m having this issue when running a rails 3.2.12 app, it is running with unicorn on production environment

Here is the unicorn log.
/home/deploy/applications/wecul-api/releases/20130606154224/config/application.rb:72:in `<class:Application>'
:
uninitialized constant Rack::Cors
 (
NameError
)
    from /home/deploy/applications/wecul-api/releases/20130606154224/config/application.rb:12:in `<module:PeepoltvApi>'
    from /home/deploy/applications/wecul-api/releases/20130606154224/config/application.rb:11:in `<top (required)>'
    from /home/deploy/applications/wecul-api/releases/20130606154224/config/environment.rb:2:in `require'
    from /home/deploy/applications/wecul-api/releases/20130606154224/config/environment.rb:2:in `<top (required)>'
    from config.ru:4:in `require'
    from config.ru:4:in `block in <main>'
    from /home/deploy/applications/wecul-api/shared/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:51:in `instance_eval'
    from /home/deploy/applications/wecul-api/shared/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:51:in `initialize'
    from config.ru:1:in `new'
    from config.ru:1:in `<main>'
    from /home/deploy/applications/wecul-api/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn.rb:44:in `eval'
    from /home/deploy/applications/wecul-api/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn.rb:44:in `block in builder'
    from /home/deploy/applications/wecul-api/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:722:in `call'
    from /home/deploy/applications/wecul-api/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:722:in `build_app!'
    from /home/deploy/applications/wecul-api/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:140:in `start'
    from /home/deploy/applications/wecul-api/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/bin/unicorn:126:in `<top (required)>'
    from bin/unicorn:16:in `load'
    from bin/unicorn:16:in `<main>'
E, [2013-06-06T21:20:28.718097 #7506] ERROR -- : reaped #<Process::Status: pid 30554 exit 1> exec()-ed
In my Gemfile I’ve added
gem 'rack-cors', :require => 'rack/cors'
In my config/application.rb I’ve added
config.middleware.use Rack::Cors do
  allow do
    origins '*'
    resource '*', :headers => :any, :methods => [:get, :post, :delete, :put, :options]
  end
end

I’d already tryied

  • Adding require 'rack/cors' in my `config/application.rb’
  • I’ve tryied this in the rails console with this output
Loading production environment (Rails 3.2.12)
irb(main):001:0> Rack::Cors
 => Rack::Cors

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Reactions: 2
  • Comments: 18 (7 by maintainers)

Most upvoted comments

You just need to un-comment the Rack CORS gem in your Gemfile (gemfile.rb)

# gemfile.rb gem 'rack-cors'

And then run the code below to install the gem bundle install

And also ensure that your cors initializer (cors.rb) is set this way # config/initializers/cors.rb

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'

    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  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.

Hope this helps

Try adding:

require ‘rack’ require ‘rack/cors’

@akinnunen @mcka1n I encountered the following issue after adding requires into application.rb. Development works but it failed to push to Heroku Please advise. LoadError: cannot load such file -- rack/cors