Forum: Ruby on Rails database.yml and encoding: utf8 -> mysql error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4d5e28840f8628550a22c410cb73ad60?d=identicon&s=25 Mattias Bud (mattias)
on 2006-05-19 15:05
I have Rails 1.1, MySql 5.0 and would like to stor text as utf8 in the
db.

I have changed the character set and collation in the db to utf8 and
utf8_danish_ci - also checked the mysql to if its ok:

show collations like "%danish%";
utf8_danish_ci	utf8	203		Yes	8

I then add the following
environment.rb:
$KCODE = 'u'
require 'jcode'

och database.yml
encoding: utf8

If I then restart the webserver I get:
Character set 'utf8' is not a compiled character set and is not
specified in the '/usr/local/mysql/share/mysql/charsets/Index' file
rake aborted!
Can't initialize character set utf8 (path:
/usr/local/mysql/share/mysql/charsets/)

First thought that I was missing the charset but it was ok.

Then I removed encoding: utf8 and added the following to my application
controller:

  before_filter :configure_charsets
  def configure_charsets
    headers["Content-Type"] = "text/html; charset=utf-8"
    ActiveRecord::Base.connection.execute 'SET NAMES UTF8'
  end

This then works fine. Has anyone else encountered the same or have a
solution?
Efe398b3b5436b0a8d4430090e8f86ea?d=identicon&s=25 seth b. (subimage)
on 2006-07-28 02:36
(Received via mailing list)
Bam! I've been looking for a solution and this seems to have fixed
everything. By default MySQL 4.1 was setup to use some weird latin
encoding.
I switched all the tables manually to utf-8 bin...Even after that on my
main
site I was getting funny characters displayed for quotes and things of
that
nature. This fixed it all.

The weird thing is, on the back-end admin site where I'm using forms and
such the data was displaying fine. I'm wondering if by default Rails
sets
this charset and content type when using the form helpers???

On 5/19/06, Mattias Bud <mattias@oncotype.dk> wrote:

>   before_filter :configure_charsets
>   def configure_charsets
>     headers["Content-Type"] = "text/html; charset=utf-8"
>     ActiveRecord::Base.connection.execute 'SET NAMES UTF8'
>   end
>
> This then works fine. Has anyone else encountered the same or have a
> solution?
>
> --
seth at subimage interactive
http://www.subimage.com/sublog/
This topic is locked and can not be replied to.