By default, the Content-type header is set to “text/html”. I’d like to add the encoding to it, so it looks like “text/html; charset=iso-8859-1”. What is the right way to do this?
So far, I’m just setting it by putting
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
in the HTML, but I don’t much like this solution.
Another way is a filter in controllers/application.rb (borrowed from RJS and Content Type Header) We use the after_filter and check the content type, which may be text/javascript for RJS.
after_filter :set_charset
def set_charset
content_type = @headers["Content-Type"] || 'text/html'
if /^text\//.match(content_type)
@headers["Content-Type"] = "#{content_type}; charset=utf-8"
end
end
Firefox seems to ignore the meta tag, the second approach works. I, too, would like to find a more central way to change the encoding…
—ChristianTreber
The above filter would be better set to this(borrowed from RJS and Content Type Header) This sets the charset for all text content types.
after_filter :set_charset
def set_charset
content_type = @headers["Content-Type"] || 'text/html'
if /^text\//.match(content_type)
@headers["Content-Type"] = "#{content_type}; charset=utf-8"
end
end
category: Howto