Printing special characters on screen in Ruby

I have a website that is working fine, except I cannot get it to print out characters other than plain text. Characters such as ', :, " and others like degrees print with the dreaded black diamond with a ? inside. Anyone has any idea as to how I might overcome this. I want to have my website printing out the above special characters.

I use MySql database to store text which is printed to the screen. This is setup with coliation = Latin1_general_ci, but changing this does not seem to mater.

My Layout has the following:-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;

<html xmlns="http://www.w3.org/1999/xhtml&quot; xml:lang="en" lang="en"> <head>   <meta http-equiv="content-type" content="text/html;charset=UTF-8" />

I have changed UTF-8 without success

The part doing the printing in View-Show is:-

  <p class="description">

  <%= @picture.description %>   </p>

class description =>

font-size:110%; line-height:1.5em; width:90%; text-align:center; margin:auto;

Don Mapp wrote:

I have a website that is working fine, except I cannot get it to print out characters other than plain text.

*Any* character is plain text.

Characters such as ', :, " and others like degrees print with the dreaded black diamond with a ? inside.

Then your actual encoding is Latin-1 (8859-1), but the browser is trying to process it as if it were UTF-8.

Anyone has any idea as to how I might overcome this. I want to have my website printing out the above special characters.

The concept of "special character" is not really a useful one. They're just characters.

I use MySql database to store text which is printed to the screen. This is setup with coliation = Latin1_general_ci, but changing this does not seem to mater.

Collation may not matter, but encoding does. Make sure your DB encoding is set to something reasonable for your data (generally UTF-8 is a good choice). MySQL also has the option of setting column encodings; make sure those are correct.

My Layout has the following:-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;

I recommend avoiding XHTML. Use HTML 4 or 5 instead. But that's a separate issue.

<html xmlns="http://www.w3.org/1999/xhtml&quot; xml:lang="en" lang="en"> <head>   <meta http-equiv="content-type" content="text/html;charset=UTF-8" />

That looks good.

I have changed UTF-8 without success

The part doing the printing in View-Show is:-

  <p class="description">

  <%= @picture.description %>   </p>

class description =>

font-size:110%; line-height:1.5em; width:90%; text-align:center; margin:auto;

The CSS is irrelevant here.

What version of Ruby are you using? 1.8 and 1.9 deal with text encodings differently.

Best,

Also,

check whether using html escaping helps:

<%= h @picture.description %>

Srikanth Shreenivas wrote:

Also, check whether using html escaping helps:

<%= *h* @picture.description %>

It won't. That's not the issue.

Best,

I am using ruby 2.35. I do not think that that will matter.

html escaping makes no difference. I have other pages with escaping set and the problem is the same.

In time I will re-write in html 5, but when I originally wrote the website years ago I do not think 4 0r 5 was available.

Please quote when replying -- otherwise the discussion becomes hard to follow.

Don Mapp wrote:

I am using ruby 2.35.

No you're not. The highest version of Ruby available is 1.9.2. Try again.

I do not think that that will matter.

If you knew what would and wouldn't matter, you'd already have been able to fix the problem.

Best,

Don Mapp wrote:

In time I will re-write in html 5, but when I originally wrote the website years ago I do not think 4 0r 5 was available.

I'd be surprised. HTML 5 is recent, but I think HTML 4 predates XHTML.

Best,

I have solved the problem. It was a simple problem, but its cause was rather involved so I will try and explain the process and knowledge I gained in solving it below:-

I first solved it on my home computer copy of my website (which I have now re-written the website in ROR 3 and Ruby1.9.2, but that will be another thread.)

I wrote a my.cnf file in my ./etc folder. I thought that that was part of the cure, but as I did not restart my server it did not have any effect until the following day when I restarted and my local copy did not start for two reasons. One was that Mysql server on my computer had stopped and (/tmp/mysql.sock) disappeared. I had to go to the preferences and restart it. The other was that both ports were set to 3000 in my my.cnf file. copy below:-

[client] port = 3000 socket = /tmp/mysql.sock default-character-set=utf8

[mysqld] port = 3306 socket = /tmp/mysql.sock skip-locking init_connect='SET collation_connection = utf8_general_ci' init_connect='SET NAMES utf8' default-character-set=utf8 character-set-server = utf8 collation-server = utf8_general_ci

[mysql] default-character-set=utf8

I think what cured it was setting encoding utf8 in my database.yml. copy below:- development:   adapter: mysql   database: donsgarden   username: root   password:   socket: /tmp/mysql.sock   encoding: utf8

I then went to my shared host and asked them to change mysql to utf8 (which they could not do) so I am stuck with Latin1 and collation Latin1_swedish_ci : I then found out that phpMyAdmin was reporting my settings wrongly, because it only uses utf8 it only reports utf8 and collation utf8_general_ci even though Mysql was using Latin1 and collation Latin1_swedish_ci throughout. (This caused a lot of head scratching) The following day my website crashed and my host moved it to a new service and all was magically solved. I think what happened was that when it restarted the encoding: utf8 in the database.yml kicked in. On my shared host my.cnf file was not changed. The only problem is that old data saved in my database is printed incorrectly, but new data entered in phpMyAdmin is saved and printed correctly. Perhaps if I changed my.cnf on my shared host (via ssh access) to include utf8 all the data (old and new as on my home copy) would print correctly. I am leaving things as they are as I can live with re-saving the little old data I have Don