Related Issues

This section is mostly about non-Rails and non-Ruby issues, but they're closely related.

Character encoding in Rails and DB migrations

See part 2 for solution of troubles described in this section

If you have an MS SQL Server database which must be migrated to MySQL, chances are good that you'll run into some nasty character encoding issues. MS SQL Server usually encodes all textual data in latin1 (ISO 8859-1, see You can, of course, use this encoding schema for your MySQL database too.

Rails, however, practically demands that you use utf-8. This is because all ajax requests are encoded in utf-8. I have actually experienced this myself, and this link confirms it:

The problem is general to all ajax related web development: it's not Rails' fault, it's XmlHttpRequest's fault. Anyway, the solution presented on the latter website did not work for me. Maybe I did not have the Ruby iconv library installed, but it felt like a hack anyway.

So, time to get the database into utf8 encoding.

Migrating the MS SQL Server database to MySQL

This part is easy. Just follow the steps described elsewhere in this document: i.e. use a tool such as Access2MySQL, which allows you to specify the character encoding for the destination MySQL database.

This should be all right? Yes, unless you want migrate you MySQL database to another MySQL database which resides on a Linux machine. The only way to do this, as far as I know, is to do a database dump (using a tool such as phpAdmin); uploading the .sql file to the Linux server; and doing a command like:


Of course, this all works. But the Unix/Linux file system is used to pipe the file data to the mysql program. And the file system, at least on my machine, does not seem to know utf8…

Probable solution: use a text editor to convert the database dump file to latin1. Do a search and replace for each table create statement.

CREATE TABLE `account_stats` (
  `id` int(11) NOT NULL auto_increment,
  `status` char(50) default NULL,
  KEY `Auto_Increment_Key` (`id`)

Create a new database on the linux machine, using this statement:


Once you've got all data transferred to the MySQL database in latin1 encoding, it's time change everything back to utf8. But this seems to involve using iconv… See

Alternative route: getting Fedora Linux to read utf8 files

If I use my text editor (pspad) to save a file in utf8 and then upload it to my fedora linux box, all special characters get messed up.

Update 20060621

The locale for my linux box was utf-8 all the time!

[root@1038 mysql]# locale

I have not idea what could be the cause of my MySQL troubles… I think using the iconv library to remedy the ajax situation might be the best idea after all…

Update 2 20060622

After some experiments, I have concluded that the database dump file is actually encoded in UTF8 now. Also, the database itself contains UTF8 text fields - if I do not start my terminal in UTF8 mode, it shows garbage when I look at the UTF8-encoded special characters. You can test this using the mysql utility to perform database queries.

(Use echo -e '\e%G' on the prompt to get your KDE or GNOME terminal into utf-8 mode).

If I use webmin to look at the database, it shows garbage too (just like in the Rails app), but that is almost certainly related to the fact that Webmin html pages contain this meta tag:

<meta http-equiv="Content-Type" content="text/html; Charset=iso-8859-1">

So, in terms of MySQL, the Webmin page encodes for latin1 instead of utf8. To fully understand this, just make an xhtml file, add some special characters encoded in utf-8 (and save the file in utf-8 format), but then use the meta tag mentioned above and see how the special characters are messed up in every single browser.

The question remains: why does Ruby on Rails have trouble showing the correct characters, even if database.yml contains this instruction:

  adapter: mysql
  database: database_name
  encoding: utf8    
  username: username
  password: password
  host: localhost

Also, my application.rhtml contains this header:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" xml:lang="en" lang="en">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

Finally, I have also included this in my environment.rb file:

# Make Ruby understand UTF-8, see
# (Added 20060621, Onno Schuit, -- I actually don't think this should # be necessary)
$KCODE = 'u'
require 'jcode'

Conclusion: somewhere along the route from database to rails to html, the utf-8 encoding gets lost. Most likely upon retrieval of the text fields. Are there any other Rails configuration files or options that I am not aware of?

It's unlikely, as the phpMyAdmin utility messes up the special characters as well.

Character encoding in Rails and DB migrations 2

I finally found the solution here:

This is what you've got to do:

[root@1038 mysql]# mysql -uUserName -pPassword --default-character-set=utf8 database_name < database_dump.sql

Take a look at the MySQL manual as well:

This explains a lot about the way MySQL interacts with clients with regard to character sets.

The MySQL server now sends the correct characters to the client. And that's really all it takes.

And if it doesn't, add encoding: utf8 to your database.yml file.

Integrating Texteditor Tinymce

Tinymce is a nice little online texteditor. The project's website,, does not really have any tutorials available, however.

Here's how you integrate Tinymce with Rails:

And in this blog I have added some comments about using Ajax in combination with TinyMCE. Don't forget to include the following code at the end of your partial:

<script type="text/javascript" >
  tinyMCE.execCommand('mceAddControl', true, 'id_of_your_textarea');  

Using the style selectbox to display your own css classes

In Tinymce you can display your own css classes in a combobox. Here's a configuration example:

    <script type="text/javascript">
        mode : "textareas",
        language : "nl",
        editor_selector : "tiny_mce",        
        theme : "advanced",        
        theme_advanced_buttons1 : "bold,underline, bullist,numlist,styleselect",        
        theme_advanced_buttons2 : " ",        
        theme_advanced_buttons3 : " ",
        auto_reset_designmode : true,
        content_css : "/stylesheets/in_editor.css"                     

Put this code in your views/layouts/application.rhtml file, or another file in views/layouts if you want to override default behavior or settings.

The setting styleselect displays the combobox. The property content_css is used to specify the contents of the combobox, as derived from a css file.

Gotchas in Prototype

The “dollar” function does not work as a shortcut for “document.getElementById” if you do not quote the id string!

E.g. :condition ⇒ ”!($(user_protect_startenddates).checked)” results in the MS IE error “user_protect_startenddates is not defined”.

Fix: rewrite as :condition ⇒ ”!($('user_protect_startenddates').checked)”.

Gotchas in Scriptaculous

Draggables stop being draggable after you have dropped them

The drag 'n drop libraries work like a charm. There's one issue though: your draggables stop being draggable after you have dropped them.

Fix: put the droppable divs before the draggable divs. This is necessary even if you position them with position:absolute in your css.


As stated on its homepage, “RMagick is an interface between the Ruby programming language and ImageMagick/ GraphicsMagick.”

You can attempt to install the prerequisite library GraphicsMagick with apt-get, but most of the time you'll get stuck with an out-of-date version (i.e. RMagick requires a newer version).

Install RMagick itself by following the instructions on this part of the homepage: Installing RMagick 2 on Linux.

Be especially sure to follow the bit of advice that says: do an irb session to test the installation.

$ irb -rubygems -r RMagick
irb(main):001:0> puts Magick::Long_version
This is RMagick 2.5.2 ($Date: 2008/08/24 21:17:09 $) Copyright (C) 2008 by Timothy P. Hunter
Built with ImageMagick 6.4.3 2008-08-26 Q8
Built for ruby 1.8.7
Web page:
=> nil

You may get a 'directory not found' type of error:

onno@mysystem:/home/familymemorygames/log# irb -rubygems -r RMagick
/opt/ruby-enterprise-1.8.6-20081215/lib/ruby/gems/1.8/gems/rmagick-2.8.0/lib/ cannot open shared object file: No such file or directory - /opt/ruby-enterprise-1.8.6-20081215/lib/ruby/gems/1.8/gems/rmagick-2.8.0/lib/ (LoadError)

The solution: use locate to find and be sure to include its path in /etc/ (the mini text editor nano is an excellent tool for this). Here's what I had to include on an Ubuntu 6.06 system:

# cat /etc/

After you've added the path to the GraphicsMagick library (i.e., be sure to issue the command ldconfig. ldconfig creates the necessary links and cache to the most recent shared libraries found in the directories specified in the file /etc/

jQuery datepicker

Here we present a completely unobtrusive way to use the jQuery datepicker plugin. Whenever javascript is disabled, the user is using the default Rails date_select select boxes. If, however, javascript is enabled, the magnificent jQuery datepicker is shown, and the Rails select boxes are completely invisible. Of course, they're still there in the background, ready to submit the parsed date to the server - preventing any ambiguities caused by user errors or confusion over date formats.

As an aside: if you're wondering how to get to the actual html dom elements when you've use jQuery's $ function:

var first_header_as_dom_element = $('div.headers').get(0);

Now let's get on with the datepicker code:

# application.html.erb

<%= javascript_include_tag "", "", "jquery.ui.datepicker-nl.js", "jquery.rails.js" %>
# _form.html.erb file (a view)

    <%= f.label :birthday %>
    <%= f.date_select :birthday, {}, :class => "date_selector"  %><%= f.text_field :birthday, :class => "ui-datepicker" %>
# In your stylesheet

input.ui-datepicker {
  width: 100px;
  display: none;
# application.js
  // Store selected date in Rails date selector fields
    onClose: function(dateText, inst) {
      var objDate = $(this).datepicker('getDate');
      $('#' + + '_1i').val( (objDate) ? objDate.getFullYear() : '');
      $('#' + + '_2i').val( (objDate) ? objDate.getMonth() + 1 : '');
      $('#' + + '_3i').val( (objDate) ? objDate.getDate() : '');
  // Use Rails date selector fields to parse date into proper format for datepicker input field
  $.each($('input.ui-datepicker'), function(index, element) {
    if (element.value != '') {
      var objDate = new Date;
      objDate.setFullYear( $('#' + + '_1i').val() );
      objDate.setMonth( $('#' + + '_2i').val() -1 );
      objDate.setDate( $('#' + + '_3i').val() );
      $(element).datepicker('setDate', objDate);
    } = 'inline';
    set_date_selector_display(, 'none')
  // use submit callback handler of your validations script if necessary

function set_date_selector_display(datepicker_id, value) {
  for(n=1;n<=3;n++) {
    $('#' + datepicker_id + '_' + n + 'i')[0].style.display = value;
    $('#' + datepicker_id + '_' + n + 'i')[0].style.visibility = 'hidden';
    $('#' + datepicker_id + '_' + n + 'i')[0].style.width = '0px';

function destroy_datepicker_fields(form) {
  // N.B.: Destroying the datepicker field does not combine well with back 
  // button - history.back().
  $.each($('input.ui-datepicker'), function(index, element) {    
    set_date_selector_display(, 'inline');
  /* Uncomment following line when using submitHandler for
   * client_side_validations plugin */
  // form.submit();
  return true;  

Personal Tools