Configuring CORS headers
Depending on how you've setup your app, you may encounter a CORS error. It will look like this in your browser console:
In this case, you need to configure the right CORS headers to allow the domain app.forestadmin.com to trigger an API call on your Application URL, which is a different domain name (e.g. localhost:3000 on development).
Rails
We use the Rack CORS Gem for this purpose.
gemfile
1
# Gemfile
2
source 'https://rubygems.org'
3
4
# ...
5
6
gem 'forest_liana'
7
gem 'rack-cors'
Copied!
config/application.rb
1
module YourApp
2
class Application < Rails::Application
3
# ...
4
5
# For Rails 5, use the class Rack::Cors. For Rails 4, you MUST use the string 'Rack::Cors'.
6
null_regex = Regexp.new(/\Anull\z/)
7
config.middleware.insert_before 0, Rack::Cors do
8
allow do
9
hostnames = [null_regex, 'localhost:4200', 'app.forestadmin.com', 'localhost:3001']
10
hostnames += ENV['CORS_ORIGINS'].split(',') if ENV['CORS_ORIGINS']
11
origins hostnames
12
resource '*',
13
headers: :any,
14
methods: :any,
15
expose: ['Content-Disposition'],
16
credentials: true
17
end
18
end
19
end
20
end
Copied!
Last modified 2mo ago
Copy link