Workaround for IE Overly Accepting in Rails’ respond_to format

Looks like Microsoft’s Internet Explorer will accept any format a web server is willing to give it.

This doesn’t play nicely with Rails’ 2.0+ respond_to feature. A slick little bit of code that asks the browser what it wants and replies accordingly.

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.”

I had ordered my code alphabetically, so ‘atom‘ came before ‘html‘, like this:

respond_to do |format|
format.atom
format.html

end

Because IE is so, um, accepting, I’ve needed to put ‘html‘ first:

respond_to do |format|
format.html
format.atom

end

For more on this issue: