Ruby on Rails 3 =>;带有特殊字符的截断方法会抛出编码不兼容错误
我需要以下方面的帮助。我在这里得到一个包含特殊字符的字符串,例如 ë、é 等。我可以在我的视图中正确显示它们,但是一旦我调用 truncate 方法,它就会抛出以下错误:
不兼容的字符编码:ASCII- 8BIT 和 UTF-8
奇怪的是,当我检查截断字符串的编码时,它确实给了我 UTF-8,这正是我所需要的(并且 UTF-8 用于我的数据库)。
my_string_with_special_characters.truncate(35).encoding.inspect
=> UTF-8
但是当我打电话时: <%= my_string_with_special_characters.truncate(35) %>
=>不兼容的字符编码:ASCII-8BIT 和 UTF-8
我还尝试了 magic_encoding gem,它在前面添加了 magic 注释 我的所有控制器文件中都有“encoding:utf-8”,但我仍然遇到不兼容的字符编码错误。
如果有人知道如何解决这个问题,请告诉我。非常感谢。
亚历克斯
I need some help with the following. I got a string here which contains special characters e.g. ë, é etc. I can display them correctly in my view but once I call the truncate method, it throws the following error:
incompatible character encodings: ASCII-8BIT and UTF-8
The weird thing is that, when I inspect the encoding of the truncated string, it does give me UTF-8, which is what I need (and UTF-8 is used for my database).
my_string_with_special_characters.truncate(35).encoding.inspect
=> UTF-8
But is is when I call:
<%= my_string_with_special_characters.truncate(35) %>
=> incompatible character encodings: ASCII-8BIT and UTF-8
I have also tried the magic_encoding gem which prepends the magic comment
"encoding : utf-8" in all of my controller files, but I still got the incompatible character encoding error.
If anyone knows how to solve this, let me know. Much appreciated.
Alex
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在文件的开头使用此字符串(对于 *.rb 文件)
Try to use this string in the beginning of you file (for *.rb files)