Can't get rack-cors working in rails application
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
How to test if Rails rack-cors gem is working
To test CORS locally, I found this repo: https://github.com/njgibbon/nicks-cors-test
Just clone it, and click on the html
file so it opens in your browser. By default it is calling the github API
(https://api.github.com) and fetching info from there. You can open the console and check it.
If you change it to https://google.com
it will throw a CORS restriction.
Similarly, you can change it to http://localhost:3000/api/v1/users
(in my case) and it will throw a CORS error with the config I have now.
To double-check, just go to cors.rb
and put origins "*"
. Restart the app server and try running again the html file. Now we won't be blocked.
Can't install rake-cors in rails-api app
I don't think rack-cors
would create a initializer. If you followed the README on Github, you have to do it yourself. Put this snippet of code into your application.rb
file.
module YourApp
class Application < Rails::Application
# ...
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
end
end
There is not an initializer in the example app as well.
gem rack-cors not working and request from angular client to rails api throwing cors error
It turns out I need to change how protect_from_forgery was handled as mentioned in docs
protect_from_forgery with: :null_session
PS: To the person who deleted his answer, please don't do that, knowing what not to do is also part of leaning.
Heroku, Rails 4, and Rack::Cors
It looks like the issue is being caused by my machine or the network I am on. I SSHed into a hosting environment I use and used the curl command above and it worked.
Additional Note
Here is something else that just happened that I thought I ought to add to this. My AJAX request was not to the https URL for my Heroku app, but Heroku was translating it be https. This was causing an additional cross-origin issue. Switching to use https for the AJAX request fixed this.
Related Topics
What's the Best Way to Talk to a Database While Using Sinatra
Cannot Execute "Rails Console" Due to an Error with Readline
How to Write a Shell Script That Starts Tmux Session, and Then Runs a Ruby Script
How to Check If a Class Is Defined
How to Handle Errors with Httparty
Scope of Constants in Ruby Modules
How to Sort a String's Characters Alphabetically
How to Use Dot Syntax for Ruby Hash
Simple Cropping with Paperclip
How to Add a New Action to the Existing Controller
How to Create a Full Audit Log in Rails for Every Table
Ruby/Rails: Convert Int to Time or Get Time from Integer
How to Loop Over a Hash of Hashes
What Do the Different Brackets in Ruby Mean