views:

469

answers:

2

While working on a project to make our site HTML 5 friendly, we were eager to embrace the new method for Cross Domain requests (no more posting through hidden iframes!!!). Using the Access Control specification we begin setting up some tests to verify the behaviour of various browsers.

The current Rails RESTful architecture relies on the four HTTP verbs: GET, POST, PUT, DELETE. However in the Access Control spec, it dictates that non-simple methods (PUT, DELETE) require a pre-flight request using the HTTP verb OPTIONS. In addition during testing we discovered that Firefox 3.5.8 pre-flight POST requests as well.

My question is this. Is anyone aware of any project for the Rails framework working to address the issue? If not, any opinions about the best strategy to support the OPTIONS method, since it has to support the routes for all the POST, PUT, DELETE methods?

A: 

I released a Gem a couple of days ago that implements CORS support via a Rack Middleware:

http://github.com/cyu/rack-cors

Regarding preflight CORS requests, I couldn't get preflight requests working in Chrome (through simple CORS requests work fine). Searching around the Internets suggests that it might not be supported. I've asked questions in the Chrome forum about this, but haven't heard a response yet.

Calvin
Can you give an example of how to use this with Rails / Sinatra? Would it go in config/initializers?
CoolAJ86
Here's how I used it in Sinatra:http://github.com/cyu/bespin_filesrb/blob/master/files.rbAs far as Rails - I'm not sure, I haven't tried it yet. I would start here though:http://guides.rubyonrails.org/rails_on_rack.html#action-controller-middleware-stack
Calvin
A: 

I hacked rails to support the options method. I posted this on the rails list, but it never made it past the list.

GitHub Gist: Rails XHR2 / CORS / OPTIONS support

ctrl+f to find the lines that have #Options - those are the only ones I changed.

And here's an example implementation | and another

CoolAJ86