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 %> <p> <b><%= column.human_name %>:</b> <%=h @post.send(column.name).reverse %> </p> <% 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? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060807/332c9a65/attachment-0001.html
On 7-aug-2006, at 7:02, Louis Simoneau wrote:> 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 %> > <p> > <b><%= column.human_name %>:</b> <%=h @post.send > ( column.name).reverse %> > </p> > <% 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 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> to the layout/posts.rhtml file, but that doesn''t help either. Anyone? On 8/7/06, Julian ''Julik'' Tarkhanov <listbox@julik.nl> wrote:> > > On 7-aug-2006, at 7:02, Louis Simoneau wrote: > > > 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 %> > > <p> > > <b><%= column.human_name %>:</b> <%=h @post.send > > ( column.name).reverse %> > > </p> > > <% 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 > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060807/e3f2bb08/attachment.html
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? On 8/7/06, Louis Simoneau <simoneau.louis@gmail.com> wrote:> > 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 > > <meta http-equiv="Content-Type" > content="text/html; charset=iso-8859-1"> > > to the layout/posts.rhtml file, but that doesn''t help either. > > Anyone? > > > On 8/7/06, Julian ''Julik'' Tarkhanov <listbox@julik.nl> wrote: > > > > > > On 7-aug-2006, at 7:02, Louis Simoneau wrote: > > > > > 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 %> > > > <p> > > > <b><%= column.human_name %>:</b> <%=h @post.send > > > ( column.name).reverse %> > > > </p> > > > <% 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 > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060808/3b1410cf/attachment.html