apache 在 Rails 应用程序中设置字符集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/901097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
set charset in rails application
提问by VP.
I'm trying to setup my charset in a html view in a RoR application.
I configured already the charset by meta equiv tag:
**meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" **
我正在尝试在 RoR 应用程序的 html 视图中设置我的字符集。我已经通过 meta equiv 标签配置了字符集:
**meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" **
It didn't work, so i tried to change my .htaccess (its a RoR application running under apache) but here is my problem. Normally i could use the following statement: AddType 'text/html; charset=ISO-8859-1' html
它不起作用,所以我尝试更改我的 .htaccess(它是在 apache 下运行的 RoR 应用程序),但这是我的问题。通常我可以使用以下语句: AddType 'text/html; 字符集=ISO-8859-1' html
But the problem is that, as everybody knows, RoR has no "file extension" and that breaks this .htaccess solution. Anybody knows another way to set a charset in a layout template or view ?
但问题是,众所周知,RoR 没有“文件扩展名”,这破坏了 .htaccess 解决方案。有人知道在布局模板或视图中设置字符集的另一种方法吗?
回答by cjs
Have your Rails application set the Content-typeheader, and then you won't need to worry about what Apache is doing.
让您的 Rails 应用程序设置Content-type标头,然后您就无需担心 Apache 正在做什么。
response.headers['Content-type'] = 'text/html; charset=utf-8'
You may also want to add
您可能还想添加
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
to the page itself, so that if someone saves it to disk, it will load with the correct charset.
到页面本身,以便如果有人将其保存到磁盘,它将使用正确的字符集加载。
回答by VP.
I added a function like that, but that still not working i have ? ~ codes in my application.rhtml that are not working.
我添加了一个这样的功能,但我仍然无法正常工作?~ 我的 application.rhtml 中的代码不起作用。
before_filter :configure_charsets
# Configuring charset to UTF-8
def configure_charsets
headers["Content-Type"] = "text/html; charset=UTF-8"
end
I added as well meta http-equiv html tag and a .htaccess parameter AddDefaultCharset UTF-8
我还添加了 meta http-equiv html 标签和一个 .htaccess 参数 AddDefaultCharset UTF-8
That's still not working, any other tip?
这仍然不起作用,还有其他提示吗?
回答by Alex
Just set :encoding => 'utf-8' after template name like this:
只需在模板名称后设置 :encoding => 'utf-8' ,如下所示:
respond_to do |f|
f.pdf do
render :pdf => 'path_to_template_file', :encoding => 'utf-8'
end
end

