我需要将字符集设置为utf-8,这似乎是默认的情况。最近,我为一些静态页面启用了页面缓存:
caches_page :about缓存工作正常,我看到在我的/public文件夹中生成了相应的about.html和contact.html页面,除了页面呈现时,它不再是utf-8格式的页面。
在谷歌了一下之后,我试着用wget查看http头文件,在缓存之前和之后:
第一次:
$wget --server-response http://localhost:3000/about
HTTP request sent, awaiting response...
1 HTTP/1.1 200 OK
2 X-Ua-Compatible: IE=Edge
3 Etag: "f7b0b4dea015140f3b5ad90c3a392bef"
4 Connection: Keep-Alive
5 Content-Type: text/html; charset=utf-8
6 Date: Sun, 12 Jun 2011 03:44:22 GMT
7 Server: WEBrick/1.3.1 (Ruby/1.8.7/2009-06-12)
8 X-Runtime: 0.235347
9 Content-Length: 5520
10 Cache-Control: max-age=0, private, must-revalidate已缓存:
$wget --server-response http://localhost:3000/about
Resolving localhost... 127.0.0.1
Connecting to localhost[127.0.0.1]:3000... connected.
HTTP request sent, awaiting response...
1 HTTP/1.1 200 OK
2 Last-Modified: Sun, 12 Jun 2011 03:34:42 GMT
3 Connection: Keep-Alive
4 Content-Type: text/html
5 Date: Sun, 12 Jun 2011 03:39:53 GMT
6 Server: WEBrick/1.3.1 (Ruby/1.8.7/2009-06-12)
7 Content-Length: 5783结果,页面显示为ISO-8859-1,我得到了一堆乱码文本。有没有人知道我怎样才能避免这种不良结果?谢谢。
发布于 2011-06-22 21:16:45
解决方案将取决于所使用的服务器。
当您使用页面缓存时,服务器直接读取服务器,因此rails堆栈不向服务器提供编码信息。然后应用服务器默认设置。
如果您正在使用apache with passenger,请添加到配置中:
AddDefaultCharset UTF-8如果需要特定的字符集,请使用类似于http://www.philsergi.com/2007/06/rails-page-caching-and-mime-types.html中的解决方案
<LocationMatch \/(rss)\/?>
ForceType text/xml;charset=utf-8
</LocationMatch>
<LocationMatch \/(ical)\/?>
ForceType text/calendar;charset=utf-8
</LocationMatch>https://stackoverflow.com/questions/6320020
复制相似问题