Monday, August 17, 2009

Fixing Rails - Ajax charset issues

If you are developing a Rails applications with Ajax for an other language than the english, and you need to use the special characters like ä, ö, ü, you may have the unpleasant surprise to experience getting unreadable characters on your HTML page.
This problem is a little pain in the ass, as it took me a couple of hours to get more understanding of it. The problem was not that their aren't many posts talking about the problem and offering many solutions, but none worked for me. I found useful this post, and this one. Go through these posts if you still don't know what the problem is about.

The solution that worked for me:

before_filter :set_charset
def set_charset
str_type = request.xhr? ? 'javascript' : 'html'
headers['Content-Type'] = "text/#{str_type}; charset=ISO-8859-1"