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?
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
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?
Found a solution:
http://www.bigbold.com/snippets/posts/show/2114
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?