Error 'incompatible character encodings: ASCII-8BIT and UTF-8' due to 8-bit encoding of cookies (Rails 3 and Ruby 1.9)

孤街醉人 提交于 2019-12-05 03:45:46

I just ran into something similar ... and found the fix hidden in the comments to this question, but think it is worth highlighting explicitly:

cookies are ASCII-8BIT but rails 3 templates are utf-8 by default. This means using a raw cookie value in a view may raise Encoding::CompatibilityError (if the user has an incompatible in the cookie value)

The fix (as noted by Adolfo Builes) is to coerce your cookie values to UTF-8, as in:

cookies["location"].force_encoding('UTF-8')
umitka

for haml put

-# coding: UTF-8

line on the top left of the page.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!