Hello, I''m a beginner to Rails. I has a problem about unicode when I tried some example code like below: say_controller.rb: ==== class SayController < ApplicationController def list @files = Dir.glob(''*'') end end ========== application.rb: ====class ApplicationController < ActionController::Base # Pick a unique cookie name to distinguish our session data from others'' session :session_key => ''_HelloRails_session_id'' before_filter :configure_charsets def configure_charsets response.headers["Content-Type"] = "text/html; charset=utf-8" # suppress(ActiveRecord::StatementInvalid) do # ActiveRecord::Base.connection.execute ''SET NAMES utf8'' # end end end ========== list.rhtml: ====<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/ TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>File List</title> </head> <body> <ul> <% for file in @files %> <li><%= file %></li> <% end %> </ul> </body> </html> ======== When there is a directory with Chinese characters, I got wrong characters (the are in GB18030), why it was not in UTF-8? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Dec 13, 2007, at 6:39 AM, xpol wrote:> I has a problem about unicode when I tried some example code like > below: > <snip> > When there is a directory with Chinese characters, I got wrong > characters (the are in GB18030), why it was not in UTF-8?I haven''t sorted out all UTF-8 yet, but I''ve learned that you need to add this to the top of the environment.rb file: $KCODE = ''UTF8'' Also, be sure to use Rails'' Multibyte API when working with UTF8 strings. http://api.rubyonrails.org/classes/ActiveSupport/Multibyte/Chars.html I don''t know if these will solve all your needs, but it''s the place to start. -- def gw acts_as_n00b writes_at(www.railsdev.ws) end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks Greg, I thikn the problem is, the @files = Dir.glob(''*'')>got strings that is not in UTF-8 encoding, and the I tried to trade them as UTF-8. There may needs conversions. BTW, is $KCODE = ''UTF8'' the same as $KCODE = ''u''>-- Best Regards! Xpol Wan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---