# Character encoding in RoR

**URL:** <https://www.ruby-forum.com/t/character-encoding-in-ror/101652>\
**Category:** Rails\
**Created:** [August 7, 2006, 7:05am UTC](https://www.ruby-forum.com/t/character-encoding-in-ror/101652 "2006-08-07T07:05:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sherwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@sherwin](https://www.ruby-forum.com/u/sherwin)\
**Post date:** [August 7, 2006, 7:05am UTC](https://www.ruby-forum.com/t/character-encoding-in-ror/101652/1 "2006-08-07T07:05:56Z")

</div>

Hi, I’m a newcomer to Ruby and to Rails, and am having a problem with  
character encoding.

Now, I know ruby doesn’t handle unicode…but, characters like Ã©  
(eacute)  
and Ã§ (ccedil) are part of the iso-8859-1 charset, so it shouldn’t be a  
problem, right?

But, for some reason, the string functions still seem to hang on those  
characters.

As an example, in a brand new rails app, with a single model “Post”  
mapped  
to a database with id, title, and body, I generate a scaffold, and  
create a  
new entry with an accented character in the title.

Then, in the show.rhtml view, I add the .reverse method as follows:

\<% for column in Post.content\_columns %\>

**\<%= column.human\_name %\>:** \<%=h @post.send(column.name).reverse %\>

\<% end %\>

The entire title is reversed, as expected, except the Ã© (eacute)  
character  
is now ?? (two question marks).

My MySQL DB is set up with latin-1 as the default. I don’t get what’s  
going  
on here.

help, anyone?

---

<div class="post-metadata">

**Author:** ![sherwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@sherwin](https://www.ruby-forum.com/u/sherwin)\
**Post date:** [August 7, 2006, 12:45pm UTC](https://www.ruby-forum.com/t/character-encoding-in-ror/101652/2 "2006-08-07T12:45:20Z")

</div>

On 7-aug-2006, at 7:02, Louis S. wrote:

> As an example, in a brand new rails app, with a single model “Post”
> 
> \<% end %\>
> 
> The entire title is reversed, as expected, except the Ã© (eacute)  
> character is now ?? (two question marks).
> 
> My MySQL DB is set up with latin-1 as the default. I don’t get  
> what’s going on here.

Did you set encoding: latin1 in database.yml ? MySQL might be  
supplying you UTF8 strings because by default it has it\s client  
charset set to UTF8.

–  
Julian ‘Julik’ Tarkhanov  
please send all personal mail to  
me at julik.nl

---

<div class="post-metadata">

**Author:** ![sherwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@sherwin](https://www.ruby-forum.com/u/sherwin)\
**Post date:** [August 7, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/character-encoding-in-ror/101652/3 "2006-08-07T16:45:37Z")

</div>

I checked my.ini in the mysql folder, it has  
default-character-set=latin1.

I also did as you recommended and set “encoding: latin1” in  
database.yml. I  
restarted both MySQL server and WEbrick. I still have the same problem.

Just in case, I added

to the layout/posts.rhtml file, but that doesn’t help either.

Anyone?

---

<div class="post-metadata">

**Author:** ![sherwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@sherwin](https://www.ruby-forum.com/u/sherwin)\
**Post date:** [August 8, 2006, 6:37am UTC](https://www.ruby-forum.com/t/character-encoding-in-ror/101652/4 "2006-08-08T06:37:52Z")

</div>

Found a solution:

> **[BigBold - Informasi Tentang Bisnis dan Marketing](http://www.bigbold.com)**
>
> BigBold ialah situs yang memberikan segala macam Informasi Tentang Bisnis dan Marketing

uses this code:

class ApplicationController \< ActionController::Base  
before\_filter :configure\_charsets

def configure\_charsets  
if request.xhr?  
@response.headers[“Content-Type”] ||= “text/javascript;  
charset=iso-8859-1”  
else  
@response.headers[“Content-Type”] ||= “text/html;  
charset=iso-8859-1”  
end  
end

end

to set the encoding. Does anyone know of a more “permanent” (i.e.  
configuration-level) solution to set this option so that non-ascii  
characters won’t be garbled by Ruby string functions?
