Ruby on Rails中的UTF-8问题

时间:2011-06-03 14:51:15

标签: ruby-on-rails encoding utf-8

我正在尝试以多种语言显示文本。因为它们都是欧洲语言,所以我选择坚持使用UTF-8编码来保存数据库和数据源。数据库是MySQL,并使用UTF-8编码进行设置。

在查看我的RoR项目的配置时,我可以找到各种设置,告诉它以UTF-8编码完成所有操作。 我存储在数据库中的数据来自一个XML文件,该文件具有UTF-8编码属性,使用notepad ++创建并以UTF-8编码保存。

但是,当我从数据库中检索数据时,输出文本中包含多个 实体。我对如何解决这个问题毫无头绪.... 以下是我的环境设置:

$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.5.2
  - RUBY VERSION: 1.8.7 (2011-02-18 patchlevel 334) [i386-mingw32]
  - INSTALLATION DIRECTORY: c:/Ruby/lib/ruby/gems/1.8
  - RUBY EXECUTABLE: c:/Ruby/bin/ruby.exe
  - EXECUTABLE DIRECTORY: c:/Ruby/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-mingw32
  - GEM PATHS:
     - c:/Ruby/lib/ruby/gems/1.8
     - c:/Users/arne.de.herdt.TENFORCE2/.gem/ruby/1.8
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

如果还有其他需要的信息,请询问,我会更新此主题。我只是想让数据正确显示。

示例:Prüferf rdieZerst rungsfreieWerkstoffpr fung(m / w)Arbeitsort:D sseldorf

修改 添加database.yml文件输出

# MySQL.  Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
#   gem install mysql2
#
# And be sure to use new-style password hashing:
#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: ESCO_development
  pool: 5
  username: hidden_username
  password: hidden_password
  host: localhost

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: ESCO_test
  pool: 5
  username: hidden_username
  password: hidden
  host: localhost

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: ESCO_production
  pool: 5
  username: hidden_username
  password: hidden
  host: localhost

1 个答案:

答案 0 :(得分:1)

您的所有数据库配置似乎都很好,您是否检查过您的浏览器是否强制执行任何字符集编码?尝试在浏览器中手动更改字符集。

此外,检查您的html文件以查找不是utf8的任何其他字符集编码