Giter Site home page Giter Site logo

uninitialized constant Rack::Cors about rack-cors HOT 18 CLOSED

cyu avatar cyu commented on July 23, 2024 2
uninitialized constant Rack::Cors

from rack-cors.

Comments (18)

promisepreston avatar promisepreston commented on July 23, 2024 15

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

from rack-cors.

akinnunen avatar akinnunen commented on July 23, 2024 5

Try adding:

require 'rack'
require 'rack/cors'

from rack-cors.

tknazn avatar tknazn commented on July 23, 2024 4

@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

from rack-cors.

cyu avatar cyu commented on July 23, 2024

What does your Gemfile look like?

from rack-cors.

blackjid avatar blackjid commented on July 23, 2024

Hi @cyu
This is my Gemfile https://github.com/peepoltv/peepoltv-api/blob/master/Gemfile#L12
And this is my config file https://github.com/peepoltv/peepoltv-api/blob/master/config/application.rb

thanks

from rack-cors.

cyu avatar cyu commented on July 23, 2024

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

from rack-cors.

cyu avatar cyu commented on July 23, 2024

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

from rack-cors.

arcreative avatar arcreative commented on July 23, 2024

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

from rack-cors.

cyu avatar cyu commented on July 23, 2024

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

from rack-cors.

cheshire137 avatar cheshire137 commented on July 23, 2024

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

from rack-cors.

cyu avatar cyu commented on July 23, 2024

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

from rack-cors.

cheshire137 avatar cheshire137 commented on July 23, 2024

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.

from rack-cors.

cyu avatar cyu commented on July 23, 2024

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?

from rack-cors.

mcka1n avatar mcka1n commented on July 23, 2024

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

from rack-cors.

aminukano585 avatar aminukano585 commented on July 23, 2024

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 :)

from rack-cors.

cyu avatar cyu commented on July 23, 2024

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

from rack-cors.

JessicaSew avatar JessicaSew commented on July 23, 2024

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

from rack-cors.

henriqueutsch avatar henriqueutsch commented on July 23, 2024

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
`

from rack-cors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.