Rails 2.3.2 / Ruby 1.8.6编码问题 - ActionController返回UTF-8? [英] Rails 2.3.2/Ruby 1.8.6 Encoding Question - ActionController returning UTF-8?

查看:134
本文介绍了Rails 2.3.2 / Ruby 1.8.6编码问题 - ActionController返回UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很简单的Rails问题,编码,我找不到一个答案。



环境:
Rails 2.3.2 / Ruby1。 8.6



我目前没有在Rails环境中设置任何编码选项,已将所有内容保留为默认值。



如果我从一个文本文件从磁盘读取一个字符串 - 并通过Rails渲染:使用Apache / Phusion的文本功能发送它,客户期望什么编码?



谢谢对于任何答案,

解决方案

由于Rails 1.2,Rails将Ruby 1.8的$ KCODE魔术变量设置为UTF8。它包括ActiveSupport :: CoreExtensions :: String :: Multibyte以修补围绕每个字符/每字节操作符的不明确的问题。你的文本文件应该是UTF-8,Ruby会传递它,你的应用程序布局应该指定一个META标签,声明文档的字符集也是UTF-8:

 < meta http-equiv =Content-Typecontent =text / html; charset = utf-8/> 

然后它应该都是只是工作,但有一些问题描述如下。

如果你在Mac上,在Terminal.app中运行script / console,然后将异常字符序列直接粘贴到终端中字符查看器是一个很好的方式来展示和演示这自己的满意,因为整个操作系统工作在UTF-8。



例如,⇒ - RIGHTWARDS DOUBLE ARROW - 是Unicode 21D2,UTF8 0xE2 (226),0x87(125),0x92(146)。如果我把它粘贴到终端并请求字节值,我得到预期的结果:

 > $ KCODE 
=> UTF8
>> ⇒
=> \342\207\222
>> puts⇒

...但...

 >> ⇒[0] 
=> 226
>> →[1]
=> 135
>> ⇒[2]
=> 146
>> ⇒[3]
=> nil

请注意,仍然使用[]获取字节访问。请参阅Rails API中的Multibyte扩展文档(适用于Rails 2.2,例如 http://railsapi.com/ ) )如果你想做字符串操作,否则像foo.reverse会做错事; foo.mb_chars.reverse通过使用mb_chars代理来正确。


I have a pretty simple Rails question regarding encoding that I can't find an answer to.

Environment: Rails 2.3.2/Ruby1.8.6

I am not setting any encoding options within the Rails environment currently, have left everything to defaults.

If I read a String from disk from a text file - and send it via Rails render :text functionality using Apache/Phusion, what encoding should the client expect?

Thank you for any answers,

解决方案

Since about Rails 1.2, Rails sets Ruby 1.8's $KCODE magic variable to "UTF8". It includes ActiveSupport::CoreExtensions::String::Multibyte to patch around issues with otherwise ambiguous per-character/per-byte operators. Your text file should be UTF-8, Ruby will pass it through and your application layout should specify a META tag declaring the document's charset to be UTF-8 too:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Then it should all 'just work', but there are some gotchas described below.

If you're on a Mac, running "script/console" in Terminal.app and then pasting unusual character sequences directly into the terminal from e.g. the Character Viewer is a good way to play around and demonstrate this to your own satisfaction, since the whole OS works in UTF-8. I don't know what the equivalent would be for Windows or an arbitrary Linux distribution.

For example, "⇒" - RIGHTWARDS DOUBLE ARROW - is Unicode 21D2, UTF8 0xE2 (226), 0x87 (125), 0x92 (146). If I paste that into Terminal and ask for the byte values I get the expected result:

>> $KCODE
=> "UTF8"
>> "⇒"
=> "\342\207\222"
>> puts "⇒"
⇒

...but...

>> "⇒"[0]
=> 226
>> "⇒"[1]
=> 135
>> "⇒"[2]
=> 146
>> "⇒"[3]
=> nil

Note how you're still getting byte access with "[]". See the documentation on the Multibyte extensions in the Rails API (for Rails 2.2, e.g. at http://railsapi.com/) if you want to do string operations, otherwise things like "foo.reverse" will do the wrong thing; "foo.mb_chars.reverse" gets it right by using the "mb_chars" proxy.

这篇关于Rails 2.3.2 / Ruby 1.8.6编码问题 - ActionController返回UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆