Arthur Chang

Entrepreneur, Software Engineer, and Photographer
« Back to blog
Aug 3
Views

Internet Explorer doesn't play nice with Rails 2+ respond_to blocks

 

One out of the millions of things wrong with Internet Explorer, is that it has strange Accept headers when sending a request to a server.  What happens in the end of it all is that IE request hits the first format you define in a respond_to block.  I found this funny transcript from http://garrickvanburen.com/archive/workaround-for-ie-overly-accepting-in-rail...

Here’s a conversation between Rails & Firefox

Firefox: “Hey Rails, I want this url”
Rails: “No problem, which format would you like it in?”
Firefox: “HTML, please.”
Rails: “Here you go.”

Here’s the same conversation with Internet Explorer

IE: “Hey Rails, I want this url”
Rails: “No problem, which format would you like it in?”
IE: “Whatcha got?”
Rails: “I’ve got Atom, and…”
IE: (interputting) “OK THANKS!”
Rails: “…um, what? I wasn’t finished, really? ok, here you go.”

The workarounds are pretty gross.  Every html request just pass along a :format => :html.  You can also make sure your AJAX calls goto .js if you put format.js blocks before anything else, or specifically set the request header for all AJAX calls as well like so:

xhr.setRequestHeader("Accept", "text/javascript" )

This google group thread is also a good quick read: http://groups.google.com/group/wellrailed/browse_thread/thread/619ad1b3c5a487cd

(photo is unrelated to topic! picture I took of my friend Anna)