I had problems, especially with Chrome. What you did is similar to what I did in my application. The only difference is that I am responding with the correct hostnames in my Origin CORS headers, not a wildcard. It seems to me that Chrome is picky about this.
Switching between development and production is a pain, so I wrote this little function that helps me in both development mode and production mode. All of the following things happen in my application_controller.rb unless otherwise specified, this may not be the best solution, but shelving does not work for me, I canβt remember why.
def add_cors_headers origin = request.headers["Origin"] unless (not origin.nil?) and (origin == "http://localhost" or origin.starts_with? "http://localhost:") origin = "https://your.production-site.org" end headers['Access-Control-Allow-Origin'] = origin headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS, PUT, DELETE' allow_headers = request.headers["Access-Control-Request-Headers"] if allow_headers.nil?
And then I have this little thing in my application_controller.rb because my site requires login:
before_filter :add_cors_headers before_filter {authenticate_user! unless request.method == "OPTIONS"}
In my routes.rb I also have this thing:
match '*path', :controller => 'application', :action => 'empty', :constraints => {:method => "OPTIONS"}
and this method looks like this:
def empty render :nothing => true end
Christoph Eicke Aug 12 '13 at 17:12 2013-08-12 17:12
source share