Ruby on Rails and UTF-8
As of September 8th, 2006, UTF-8 support in Ruby and Ruby on Rails is incomplete. Many string manipulation functions in Ruby will give unexpected results when used on UTF-8 strings, as this Rails console session demonstrates:
>> "estás".length
=> 6
However, a Rails application is still capable of accepting and outputting UTF-8 text.
Here are the current best practices:
Set your database to UTF-8
Use UTF-8 for all database tables (not strictly necessary, but will give you proper searching and sorting results):
# database.yml
development:
adapter: mysql
database: example_development
encoding: utf8
username: root
password:
Set the browser to UTF-8
It's important to tell the web browser that you are outputting UTF-8. You may be tempted to add this to your layout:
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
However this will not work for AJAX updates to your web pages in some browsers (IE, Safari). A more complete solution is to add this to the headers of all HTTP requests:
Content-Type: text/html; charset=UTF-8
You can do this via your web server's configuration, or you can add the following to your Rails app:
# application.rb
after_filter :set_charset
def set_charset
@headers["Content-Type"] ||= "text/html; charset=UTF-8"
end