Comments (18)
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.
Try adding:
require 'rack'
require 'rack/cors'
from 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
from rack-cors.
What does your Gemfile look like?
from rack-cors.
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.
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.
Closing this issue. Please re-open if you're still having issues.
from rack-cors.
Having the same issue as OP, using the string doesn't work for me either.
from rack-cors.
@arcreative Are you still having this issue? And you've re-started your app after adding the gem?
from rack-cors.
Having the same issue in a Rails 4 app in production environment.
from rack-cors.
@moneypenny Can you send post the Gemfile and the application.rb (or wherever you're using the middleware)
from rack-cors.
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.
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.
+1 @akinnunen It makes sense to add those requires before use Rack::Cors do
. It worked for me.
from 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()-edIn my
Gemfile
I've addedgem 'rack-cors', :require => 'rack/cors'In my
config/application.rb
I've addedconfig.middleware.use Rack::Cors do allow do origins '*' resource '*', :headers => :any, :methods => [:get, :post, :delete, :put, :options] end endI'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.
I'm closing this issue because there hasn't been any activity on this issue in a while.
from rack-cors.
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.
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)
- Tag for v2.0.0 is missing HOT 3
- Does not work with Rack 3.0.0 HOT 1
- My CORS setup blocks bad urls but not POSTMAN requests HOT 3
- Vulnerabilities in included mocha.js HOT 1
- Stripe api with Cloud9 setup
- Is there any CI? HOT 1
- Cookie session related middleware interferes HOT 3
- Remove/Replace Rack::Utils::HeaderHash HOT 7
- delete the confusing `2.0.0` tag since `v2.0.0` exists? HOT 1
- issue comeback duplicate headers because of header name case HOT 3
- Release version 2.0.1 HOT 2
- How to add Access-Control-Allow-Private-Network header in Option response?
- Why is Rails.application.config.hosts required? HOT 1
- Regex origin woes HOT 2
- Upgrade to rails 7.1.1
- Ressource with $ are not handled as expected
- Access-Control-Allow-Origin is returned only if correct Origin header is available
- Documentation for the frame-ancestors directive?
- [SECURITY] CVE-2024-27456 - Insecure File Permissions in rack-cors v2.0.1 HOT 15
- The browser does not store the cookies session
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rack-cors.