Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uninitialized constant Rack::Cors #26

Closed
blackjid opened this issue Jun 6, 2013 · 18 comments
Closed

uninitialized constant Rack::Cors #26

blackjid opened this issue Jun 6, 2013 · 18 comments

Comments

@blackjid
Copy link

blackjid commented Jun 6, 2013

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
@cyu
Copy link
Owner

cyu commented Jun 7, 2013

What does your Gemfile look like?

@blackjid
Copy link
Author

blackjid commented Jun 7, 2013

@cyu
Copy link
Owner

cyu commented Jun 7, 2013

Try replacing the class reference w/ a string:

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

@cyu
Copy link
Owner

cyu commented Aug 21, 2014

Closing this issue. Please re-open if you're still having issues.

@cyu cyu closed this as completed Aug 21, 2014
@arcreative
Copy link

Having the same issue as OP, using the string doesn't work for me either.

@cyu
Copy link
Owner

cyu commented Sep 24, 2014

@arcreative Are you still having this issue? And you've re-started your app after adding the gem?

@cheshire137
Copy link

Having the same issue in a Rails 4 app in production environment.

@cyu
Copy link
Owner

cyu commented Sep 26, 2014

@moneypenny Can you send post the Gemfile and the application.rb (or wherever you're using the middleware)

@cheshire137
Copy link

Sorry for the delay. Here's our Gemfile:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.5'

# Use pg as the database for Active Record
gem 'pg', '~> 0.17.1'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
gem 'bootstrap-sass', '~> 3.1.1'
gem 'font-awesome-sass', '~> 4.1.0'

# Presentation
gem 'haml', '~> 4.0.5'
gem 'haml-rails', '~> 0.5.3'
gem 'jbuilder', '~> 2.0.3'
gem 'liquid', '~> 2.6.1'
gem 'kramdown', '~> 1.4.0'

# For copying text
gem 'zeroclipboard-rails', '~> 0.1.0'

# For diffs
gem 'codemirror-rails', '~> 4.5'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails', '~> 3.1.1'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks', '~> 2.2.2'
gem 'jquery-turbolinks', '~> 2.0.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

# for configuration
gem 'dotenv-rails', '~> 0.11.1'

# Use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.1.2'

# Authentication
gem 'devise', '~> 3.2.2'
gem 'omniauth-oauth2', '~> 1.1.2'

# Authorization
gem 'cancancan', '~> 1.8'

# Background processing
gem 'sidekiq', '~> 3.0.0'

# For exception notification
gem 'airbrake', '~> 4.0.0'

gem 'git', '~> 1.2.7'
gem 'ace-rails-ap', '~> 2.0.1'
gem 'jekyll', '~> 2.1.1'

# For website publishing state
gem 'aasm', '~> 3.3.1'

# Friendly JavaScript editor
gem 'tinymce-rails'

gem 'rake', '~> 10.3.2'

gem 'fog', '~> 1.23.0'

gem 'rack-cors', '~> 0.2.9', require: 'rack/cors'

group :development do
  # Spring speeds up development by keeping your application running in the
  # background. Read more: https://github.com/rails/spring
  gem 'spring'

  gem 'spring-commands-rspec'
  gem 'awesome_print', '~> 1.2.0'
  gem 'pry-rails', '~> 0.3.2'
  gem 'thin', '~> 1.6.2'
  gem 'foreman', '~> 0.74.0'

  gem 'capistrano', '~> 3.2.0'
  gem 'capistrano-rails', '~> 1.1.1'
  gem 'capistrano-bundler', '~> 1.1.2'
  gem 'capistrano-sidekiq', '~> 0.3.3'
end

group :development, :test do
  # Use debugger
  # gem 'debugger'

  gem 'rspec-rails', '~> 3.0.2'
  gem 'rspec-collection_matchers', '~> 1.0.0'
  gem 'factory_girl_rails', '~> 4.4.1'
  gem 'database_cleaner', '< 1.1.0'
  gem 'simplecov', '~> 0.9.0', require: false
  gem 'capybara'
  gem 'capybara-webkit'
end

gem 'unicorn', '~> 4.8.3'

And config/application.rb from the Rails app:

require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(:default, Rails.env)
Dotenv.overload('dotenv.test') if Rails.env.test?
module MyApp
  class Application < Rails::Application
    config.middleware.insert_before 'ActionDispatch::Static', 'Rack::Cors' do
      allow do
        origins '*'
        resource '*', headers: :any,
                      methods: [:get, :post, :options, :delete, :head, :put],
                      max_age: 0
      end
    end
    config.generators do |g|
      g.test_framework :rspec
    end
    config.autoload_paths << Rails.root.join('lib')
  end
end

In config/environments/production.rb, we have config.serve_static_assets = true.

@cyu
Copy link
Owner

cyu commented Oct 9, 2014

Everything looks okay... I have a example app (in the examples directory) using Rails 4.1.5 and starts up okay. How about the backtrace? What does that look like?

@cyu cyu reopened this Oct 9, 2014
@akinnunen
Copy link

Try adding:

require 'rack'
require 'rack/cors'

@mcka1n
Copy link

mcka1n commented May 18, 2015

+1 @akinnunen It makes sense to add those requires before use Rack::Cors do. It worked for me.

@tknazn
Copy link

tknazn commented Aug 5, 2016

@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

@aminukano585
Copy link

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

Your bug happens to be my solution, odd enough :)

@promisepreston
Copy link

promisepreston commented Apr 16, 2019

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

@cyu
Copy link
Owner

cyu commented May 7, 2020

I'm closing this issue because there hasn't been any activity on this issue in a while.

@cyu cyu closed this as completed May 7, 2020
@JessicaSew
Copy link

I'm having an issue when starting up my server with shotgun:
Boot Error
Something went wrong while loading config.ru

LoadError: cannot load such file -- rack/cors

@henriqueutsch
Copy link

I'm having this issue too and this code resolv for me.

`
app_urls = ENV.fetch('APP_ORIGIN_URL', '').to_s.split(',').compact

if app_urls.present?
require 'rack'
require 'rack/cors'

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

  resource '*',
           headers: :any,
           methods: [:get, :post, :put, :patch, :delete, :options, :head]
end

end
end
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests