xpol
#1
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:
File List
<% for file in @files %>
- <%= file %>
<% end %>
=========
When there is a directory with Chinese characters, I got wrong
characters (the are in GB18030), why it was not in UTF-8?
xpol
#2
On Dec 13, 2007, at 6:39 AM, xpol wrote:
I has a problem about unicode when I tried some example code like
below:
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
xpol
#3
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