I have a controller action in my Rails 2.0.2 app that looks like this:
def matrix
@shows = Show.find(:all)
@players = Player.find(:all)
respond_to do |format|
format.html
format.xls do
set_excel_headers(headers,"matrix-#{@show_year}-#{@show_month}.xls")
end
end
end
In Firefox, this works exactly as expected - when I go to /signups/matrix, I get the HTML format rendered. If I go to /signups/matrix.xls, I get the Excel version.
However, when I do the same thing on IE6/XP, I get the Excel format rendered for both. /signups/matrix tries to download the Excel file.
The log doesn't show anything odd that I can tell:
The key to this behavior is the Accept header of the HTTP request. You can
check it in Rails with request.headers['HTTP_ACCEPT'] (see
ActionController::AbstractRequest).
IE6 prefers MS Office content (Excel, Powerpoint, Word) over HTML:
...
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/vnd.ms-excel,
application/msword, application/vnd.ms-powerpoint, */*
...
(I got this from
http://www.ibm.com/developerworks/xml/library/x-tipapachexhtml/index.html
as I don't have IE6 installed.)
So (X)HTML mimetypes don't even appear explicitly, they're caught by the
wildcard */* and have the lowest priority.
Very strange, but this is how Microsoft wanted it to be. They fixed this in
the meantime. The Internet Explorer 8 beta header says simply:
Accept: */*
To be sure to get the HTML rendering, say /signups/matrix.html . I guess
you could also tinker with your HTTP server settings to give the HTML
mimetypes higher priority.
Thanks for the tip - I went in and changed application/vnd.ms-excel to
application/octet-stream in the initializer/mime_types.rb file, and it
seems to be performing expected behavior now.