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:
Processing SignupsController#matrix (for 192.168.119.1 at 2008-05-14
20:03:57) [GET]
Session ID: 39a88d1fff075058276e65d67136f7dd
Parameters: {“action”=>“matrix”, “controller”=>“signups”}
[…]
Completed in 0.23381 (4 reqs/sec) | Rendering: 0.03932 (16%) | DB:
0.09844 (42%) | 200 OK [http://192.168.119.1/signups/matrix]
I’m stumped - does anyone know why this would be happening?
–Wade
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.
Thanks again!
–Wade