Search code examples
ruby-on-railsinternet-explorerxdomainrequestxdr

Rails - How to fake PUT or DELETE request using XDomainRequest in IE


How can I fake a PUT or DELETE request using XDomainRequest in IE? Or do I need to use an iframe transport?

I'm trying to access a restful API that's setup for CORS. It works in all other browsers but I can't figure out how to fake the PUT/DELETE actions in IE. With XDomainRequest, custom headers are not allowed, so I can't add the HTTP_X_HTTP_METHOD_OVERRIDE header which supposedly tells Rails to recognize the _method=put parameter in the json data.


Solution

  • The best solution I could think of was to add two new member routes mapped to #update and #destroy:

    resources :posts do
        member do
            post :revise, :action => :update
            post :annihilate, :action => :destroy
        end
    end
    

    which added these routes when you run 'rake routes':

        revise_post POST   /posts/:id/revise(.:format)     {:action=>"update", :controller=>"posts"}
    annihilate_post POST   /posts/:id/annihilate(.:format) {:action=>"destroy", :controller=>"posts"}
    

    Note that I originally tried this:

    resources :posts do
        member do
            post :update
            post :destroy
        end
    end
    

    hoping that it would create these routes:

     update_post POST   /posts/:id/update(.:format)  {:action=>"update", :controller=>"posts"}
    destroy_post POST   /posts/:id/destroy(.:format) {:action=>"destroy", :controller=>"posts"}
    

    but instead it created:

    POST   /posts/:id(.:format) {:action=>"update", :controller=>"posts"}
    POST   /posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
    

    which look like they are overlapping and you could never get to posts#destroy.