为什么我升级到 Rails 3 后所有字符串都是 ASCII-8BIT? [英] Why are all strings ASCII-8BIT after I upgraded to Rails 3?
问题描述
我升级到 RoR 3.0.1,Ruby 升级到 1.9.2.现在我视图中的所有字符串都是 ASCII-8BIT?
I upgraded to RoR 3.0.1 and Ruby to 1.9.2. Now all the strings in my views are ASCII-8BIT?
我相信我的应用已设置为使用 UTF 8
I believe I have my app set up to use UTF 8
application.rb
config.encoding = "utf-8"
database.yml
development:
adapter: mysql
encoding: utf8
我在跑步
OS X
RVM rvm 1.0.16
Ruby ruby-1.9.2-p0
Rails 3.0.1
我希望编码是 UTF 8 而不是 ASCII
business.desc.encoding
# ASCII-8BIT
由于 1.9.x 可以连接不同编码的字符串,我们看到很多这样的错误.
Since 1.9.x can concatenate strings of different encodings we see a lot of errors like this.
<p class="description"><%= truncate(business.desc, :length => 17) %></p>
错误
incompatible character encodings: ASCII-8BIT and UTF-8
activesupport (3.0.1) lib/active_support/core_ext/string/output_safety.rb:74:in `concat'
activesupport (3.0.1) lib/active_support/core_ext/string/output_safety.rb:74:in `concat'
actionpack (3.0.1) lib/action_view/template/handlers/erb.rb:14:in `<<'
app/views/browse/businesses.html.erb:15:in `block in _app_views_browse_businesses_html_erb___1616718260917998189_2173630500__1134905695726531092'
app/views/browse/businesses.html.erb:3:in `each'
app/views/browse/businesses.html.erb:3:in `each_with_index'
app/views/browse/businesses.html.erb:3:in `_app_views_browse_businesses_html_erb___1616718260917998189_2173630500__1134905695726531092'
其他人有这个问题吗?ruby-1.9.2-p0 是要使用的正确版本吗?
Does anyone else have this problem? Is ruby-1.9.2-p0 the correct version to use?
谢谢!
推荐答案
您需要将此添加到每个 .rb 文件中:
You need to add this to every .rb file:
<% # coding: UTF-8 %>
为此我使用 gem magic_encoding.
I use the gem magic_encoding for that.
$ cd app/
$ magic_encoding
默认为 UTF-8,但您可以指定任何您想要的参数作为参数.
The default is UTF-8, but you can specify whatever you want as an argument.
这篇关于为什么我升级到 Rails 3 后所有字符串都是 ASCII-8BIT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!