Skip to content

Instantly share code, notes, and snippets.

@GA-MEB
Last active August 29, 2015 14:22
Show Gist options
  • Save GA-MEB/5d7904479b200ec30ec0 to your computer and use it in GitHub Desktop.
Save GA-MEB/5d7904479b200ec30ec0 to your computer and use it in GitHub Desktop.

General Assembly Logo

This is a quick and dirty way of implementing CORS in a Rails app. DO NOT DO THIS IN A PROFESSIONAL SETTING

  1. Go to app/controllers/application_controller.rb and comment out the protect_from_forgery line.
  2. Go to your Gemfile and add the following line to the bottom : gem 'rack-cors', :require => 'rack/cors'
  3. Run bundle
  4. Go to config/application.rb and add the following code to the bottom of the Application class definition:
    config.middleware.use Rack::Cors do
      allow do
        origins '*'
        resource '*', :headers => :any, :methods => [:get, :post, :put, :patch, :delete, :options]
      end
    end
  1. If you haven't already, restart the Rails server; then, serve up your front-end and start making requests!

Versions

Ruby : 2.2.0
Rails: 4.2.1
Bundler: 1.10.2
Postgres: 9.4.2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment