开发者

Ruby on Rails 3 => truncate method with special characters throws Encoding Incompatability error

开发者 https://www.devze.com 2023-02-22 02:17 出处:网络
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 metho开发者_开发百科d,

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 metho开发者_开发百科d, 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


Try to use this string in the beginning of you file (for *.rb files)

# -*- encoding: utf-8 -*-
0

精彩评论

暂无评论...
验证码 换一张
取 消