3

我需要 charset 为 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 显示,我得到一堆乱码。有谁知道我可以如何防止这种不良结果?谢谢你。

4

1 回答 1

2

解决方案将取决于所使用的服务器。

当您使用页面缓存时,服务器会直接读取服务器,因此 rails 堆栈不会向服务器提供编码信息。然后服务器默认应用。

If you're using apache with passenger, add to the configuration:

AddDefaultCharset UTF-8

If you need specific charsets, use a solution like the one in 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>
于 2011-06-22T13:16:45.253 回答