好的,我现在有一些可以工作的东西。感谢@danivovich 让我在正确的地方开始。我要做的第一件事是整理 mime_types.rb 中的 Mime 类型,这样 HTML 就不会被 XHTML 别名:
module Mime
remove_const('HTML') # remove this so that we can re-register the types
end
Mime::Type.register "text/html", :html
Mime::Type.register "application/xhtml+xml", :xhtml
我刚刚将它添加到我的应用程序控制器中:
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 makes sure that XHTML is always servered as such, unless the client doesn't accept it, or HTML has been explicitly requested. HTML is always just XHTML served as a polyglot. The @serving_polyglot variable is available in the views where any switching is needed.
This is working for me under Chrome, Safari, Firefox, Opera and IE[6-8].