huacnlee / rails-settings-cached

Global settings for your Rails application.
Other
1.06k stars 202 forks source link

ActionView::Template::Error: incompatible character encodings: UTF-8 and ASCII-8BIT #53

Closed kalashnikovisme closed 5 years ago

kalashnikovisme commented 9 years ago

I have error:

ActionView::Template::Error: incompatible character encodings: UTF-8 and ASCII-8BIT

Rails -v 4.1.7 Ruby 2.1.2p95

Any idea?

kalashnikovisme commented 9 years ago

I've solved this problem by adding _forceencoding method

Settings.defaults['foo'].force_encoding('UTF-8')

This is not best practice. Can anyone offer better solution?

Sudrien commented 9 years ago

You either put

encoding: utf-8

at the beginning of your files, Or set the encoding in your text editor - it varies by editor, but I've had the most success with "UTF8 no Byte Order Marking"

kalashnikovisme commented 9 years ago

@Sudrien but what is "UTF8 no Byte Order Marking"?

Sudrien commented 9 years ago

@kalashnikovisme - sorry, I got the name slightly wrong; It's just "Byte Order Mark", or BOM for short.

The long version is http://unicode.org/faq/utf_bom.html

The short version: Some versions of Unicode allow bytes to be ordered differently - most significant byte first, least significant byte first. "Big Endian" and "Little Endian" are alternate names. These versions have a Byte Order Mark at the beginning of the file to say which variation of Unicode is being used.

UTF8 can have this mark, but does not have to. In fact, the mark can confuse some interpreters. So if your text editor has a URF8 encoding option, say yes to that, but if it has further options to use BOM, I'd recommend saying no to that option.