mysql -uUSER -pPASSWORD DATABASE_NAME < FILENAME.sql
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`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
Create a new database on the linux machine, using this statement:
CREATE DATABASE `database_name` DEFAULT CHARACTER SET latin1;
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 [[http://www.loudthinking.com/arc/000415.html|http://www.loudthinking.com/arc/000415.html]]
====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
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
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:
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:
development:
adapter: mysql
database: database_name
encoding: utf8
username: username
password: password
host: localhost
Also, my ''**application.rhtml**'' contains this header:
Finally, I have also included this in my ''**environment.rb**'' file:
# Make Ruby understand UTF-8, see http://manuals.rubyonrails.com/read/chapter/105
# (Added 20060621, Onno Schuit, www.solin.nl -- 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:
[[http://bruno.vernay.free.fr/wp/?cat=3|http://bruno.vernay.free.fr/wp/?cat=3]]
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:
[[http://dev.mysql.com/doc/refman/4.1/en/charset-connection.html|http://dev.mysql.com/doc/refman/4.1/en/charset-connection.html]]
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, [[http://tinymce.moxiecode.com/example_full.php?example=true|http://tinymce.moxiecode.com/example_full.php?example=true]], does not really have any tutorials available, however.
Here's how you integrate Tinymce with Rails:
[[http://www.busyashell.com/blog/articles/2006/05/03/tiny-mce-living-in-your-rails-forms|http://www.busyashell.com/blog/articles/2006/05/03/tiny-mce-living-in-your-rails-forms]]
And in [[http://www.crossedconnections.org/w/?p=88|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:
====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:
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.
===== RMagick =====
As stated on its [[http://rmagick.rubyforge.org/|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: [[http://rmagick.rubyforge.org/install2-linux.html|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 http://www.imagemagick.org
Built for ruby 1.8.7
Web page: http://rmagick.rubyforge.org
Email: rmagick@rubyforge.org
=> 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/RMagick2.so: libMagickCore.so.1: 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/RMagick2.so (LoadError)
The solution: use ''locate'' to find ''libMagickCore.so.1'' and be sure to include its path in ''/etc/ld.so.conf'' (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/ld.so.conf
/usr/local/lib/
After you've added the path to the GraphicsMagick library (i.e. ''libMagickCore.so.1''), 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/ld.so.conf''.
===== 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 "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js", "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
$(document).ready(function(){
// Store selected date in Rails date selector fields
$('input.ui-datepicker').datepicker({
onClose: function(dateText, inst) {
var objDate = $(this).datepicker('getDate');
$('#' + this.id + '_1i').val( (objDate) ? objDate.getFullYear() : '');
$('#' + this.id + '_2i').val( (objDate) ? objDate.getMonth() + 1 : '');
$('#' + this.id + '_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( $('#' + element.id + '_1i').val() );
objDate.setMonth( $('#' + element.id + '_2i').val() -1 );
objDate.setDate( $('#' + element.id + '_3i').val() );
$(element).datepicker('setDate', objDate);
}
element.style.display = 'inline';
set_date_selector_display(element.id, 'none')
});
// use submit callback handler of your validations script if necessary
$('form').submit(destroy_datepicker_fields);
$.datepicker.setDefaults($.datepicker.regional['nl']);
});
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(element.id, 'inline');
$(element).remove();
});
/* Uncomment following line when using submitHandler for
* client_side_validations plugin */
// form.submit();
return true;
}