11th
Sep
Rails' respond_to and IE ACCEPT header
I’ve been using rails respond_to feature to render Excel spreadsheet alternatives to the standard HTML. The controller snippet is something like this:
respond_to do |format|
format.html
format.xls
end
Everything is fine until trying to look at the page in IE - instead of just getting the HTML as expected, the Excel spreadsheet is returned instead. Why? For some reason (presumably to do with the close integration with Windows), Internet Explorer doesn’t really want you to give it HTML, despite being a web browser. It would rather have an image or an office document:
ACCEPT: image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, application/xaml+xml,
application/vnd.ms-xpsdocument, application/x-ms-xbap,
application/x-ms-application, */*
Compare this to the Firefox ACCEPT header:
text/html,application/xhtml+xml,
application/xml;q=0.9,*/*;q=0.8
The fix I found is as follows:
format.xls if !request.env["HTTP_USER_AGENT"].include?("MSIE")
|| params[:format] == "xls"