Umlauts (special characters) and AJAX
March 25th, 2006 by papillon
There are two things that need to be done when you want to (or, in my case, have to) use an ISO character set throughout your application (i.e. database and web pages).
1) Set the character encoding to ISO-8859-1 in the HTTP header. From application.rb:
before_filter :set_charset def set_charset @headers["Content-Type"] = "text/html; charset=ISO-8859-1" end
(See “Foreign language” characters below)
2) Convert data sent via XMLHTTPREQUEST from UTF-8 to ISO-8859-1
Get the iconv library (instructions) (might already be included with Ruby 1.8.4).
Create a new file charset_conversion.rb in the helpers/ directory:
require 'iconv'
class Hash
def iconv!(to,from)
iconv = Iconv.new(to,from)
perform_iconv!(iconv)
iconv.close
end
def perform_iconv!(iconv)
each_pair do |key,value|
case value
when String
self[key] = iconv.iconv(value)
when Hash
value.perform_iconv!(iconv)
end
end
end
end
Include this file in application_helper.rb:
require 'charset_conversion'
In the action that receives the serialized XML data, add a call to iconv! before using the data:
def update
params.iconv!("iso-8859-1", "utf-8")
..
This is essentially my interpretation of the notes from here: Ajax and Character Sets