Ok, I have something that works now. Thanks @danivovich for starting me in the right place. The first thing I needed to do was sort the Mime types in mime_types.rb so that HTML was not an alias with XHTML:
module Mime
remove_const('HTML')
end
Mime::Type.register "text/html", :html
Mime::Type.register "application/xhtml+xml", :xhtml
I just added this to my application controller:
before_filter :negotiate_xhtml
after_filter :set_content_type
def negotiate_xhtml
@serving_polyglot = false
if params[:format].nil? or request.format == :html
@serving_polyglot = ((not request.accepts.include? :xhtml) or params[:format] == 'html')
request.format = :xhtml
end
end
def set_content_type
if @serving_polyglot
response.content_type = 'text/html'
end
end
This ensures that XHTML is always the server as such if the client does not accept it or if HTML is explicitly requested. HTML is always just XHTML served as a polyglot. The @serving_polyglot variable is available in views where any switching is required.
This works for me in Chrome, Safari, Firefox, Opera, and IE [6-8].
source
share