不兼容的字符编码:ASCII-8BIT和UTF-8


72

我使用Ruby 1.9.2和Rails 3.0.5

我有以下错误:

不兼容的字符编码:ASCII-8BIT和UTF-8

我认为它与数据库无关。

错误出现在视图的这一行上(只是一个div haml调用):

#content

全栈:

    ActionView::Template::Error (incompatible character encodings: ASCII-8BIT and UTF-8):
        21:                     -flash.each do |name, msg|
        22:                         =content_tag :div, msg, :id => "flash_#{name}"
        23:                         %div.clear                      
        24:                     #content                                        
        25:                         = yield
        26:             = render :partial => "layouts/grid_right" if render_grid_right?
        27:             = render :partial => "layouts/footer"
      app/views/layouts/application.html.haml:24:in `_app_views_layouts_application_html_haml___4380000789490545718_2180251300_2717546578298801795'
      actionpack (3.0.5) lib/action_view/template.rb:135:in `block in render'
      activesupport (3.0.5) lib/active_support/notifications.rb:54:in `instrument'
      actionpack (3.0.5) lib/action_view/template.rb:127:in `render'
      actionpack (3.0.5) lib/action_view/render/layouts.rb:80:in `_render_layout'
      actionpack (3.0.5) lib/action_view/render/rendering.rb:62:in `block in _render_template'
      activesupport (3.0.5) lib/active_support/notifications.rb:52:in `block in instrument'
      activesupport (3.0.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
      activesupport (3.0.5) lib/active_support/notifications.rb:52:in `instrument'
      actionpack (3.0.5) lib/action_view/render/rendering.rb:56:in `_render_template'
      actionpack (3.0.5) lib/action_view/render/rendering.rb:26:in `render'
      haml (3.0.25) lib/haml/helpers/action_view_mods.rb:13:in `render_with_haml'
      actionpack (3.0.5) lib/abstract_controller/rendering.rb:115:in `_render_template'
      actionpack (3.0.5) lib/abstract_controller/rendering.rb:109:in `render_to_body'
      actionpack (3.0.5) lib/action_controller/metal/renderers.rb:47:in `render_to_body'
      actionpack (3.0.5) lib/action_controller/metal/compatibility.rb:55:in `render_to_body'
      actionpack (3.0.5) lib/abstract_controller/rendering.rb:102:in `render_to_string'
      actionpack (3.0.5) lib/abstract_controller/rendering.rb:93:in `render'
      actionpack (3.0.5) lib/action_controller/metal/rendering.rb:17:in `render'
      actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:40:in `block (2 levels) in render'
      activesupport (3.0.5) lib/active_support/core_ext/benchmark.rb:5:in `block in ms'
      /Users/michaelkoper/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
      activesupport (3.0.5) lib/active_support/core_ext/benchmark.rb:5:in `ms'
      actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:40:in `block in render'
      actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:78:in `cleanup_view_runtime'
      activerecord (3.0.5) lib/active_record/railties/controller_runtime.rb:15:in `cleanup_view_runtime'
      actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:39:in `render'
      actionpack (3.0.5) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
      actionpack (3.0.5) lib/action_controller/metal/mime_responds.rb:261:in `block in retrieve_response_from_mimes'
      actionpack (3.0.5) lib/action_controller/metal/mime_responds.rb:192:in `call'
      actionpack (3.0.5) lib/action_controller/metal/mime_responds.rb:192:in `respond_to'
      app/controllers/home_controller.rb:9:in `index'
      actionpack (3.0.5) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
      actionpack (3.0.5) lib/abstract_controller/base.rb:150:in `process_action'
      actionpack (3.0.5) lib/action_controller/metal/rendering.rb:11:in `process_action'
      actionpack (3.0.5) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
      activesupport (3.0.5) lib/active_support/callbacks.rb:445:in `_run__3968431659371141392__process_action__3163094469870857953__callbacks'
      activesupport (3.0.5) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
      activesupport (3.0.5) lib/active_support/callbacks.rb:93:in `run_callbacks'
      actionpack (3.0.5) lib/abstract_controller/callbacks.rb:17:in `process_action'
      actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
      activesupport (3.0.5) lib/active_support/notifications.rb:52:in `block in instrument'
      activesupport (3.0.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
      activesupport (3.0.5) lib/active_support/notifications.rb:52:in `instrument'
      actionpack (3.0.5) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
      actionpack (3.0.5) lib/action_controller/metal/rescue.rb:17:in `process_action'
      actionpack (3.0.5) lib/abstract_controller/base.rb:119:in `process'
      actionpack (3.0.5) lib/abstract_controller/rendering.rb:41:in `process'
      actionpack (3.0.5) lib/action_controller/metal.rb:138:in `dispatch'
      actionpack (3.0.5) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
      actionpack (3.0.5) lib/action_controller/metal.rb:178:in `block in action'
      actionpack (3.0.5) lib/action_dispatch/routing/route_set.rb:62:in `call'
      actionpack (3.0.5) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
      actionpack (3.0.5) lib/action_dispatch/routing/route_set.rb:27:in `call'
      rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in `block in call'
      rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:in `block in recognize'
      rack-mount (0.6.13) lib/rack/mount/code_generation.rb:68:in `optimized_each'
      rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:in `recognize'
      rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:in `call'
      actionpack (3.0.5) lib/action_dispatch/routing/route_set.rb:492:in `call'
      haml (3.0.25) lib/sass/plugin/rack.rb:41:in `call'
      actionpack (3.0.5) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
      actionpack (3.0.5) lib/action_dispatch/middleware/head.rb:14:in `call'
      rack (1.2.1) lib/rack/methodoverride.rb:24:in `call'
      actionpack (3.0.5) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
      actionpack (3.0.5) lib/action_dispatch/middleware/flash.rb:182:in `call'
      actionpack (3.0.5) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
      actionpack (3.0.5) lib/action_dispatch/middleware/cookies.rb:302:in `call'
      activerecord (3.0.5) lib/active_record/query_cache.rb:32:in `block in call'
      activerecord (3.0.5) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
      activerecord (3.0.5) lib/active_record/query_cache.rb:12:in `cache'
      activerecord (3.0.5) lib/active_record/query_cache.rb:31:in `call'
      activerecord (3.0.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:354:in `call'
      actionpack (3.0.5) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
      activesupport (3.0.5) lib/active_support/callbacks.rb:415:in `_run_call_callbacks'
      actionpack (3.0.5) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
      rack (1.2.1) lib/rack/sendfile.rb:107:in `call'
      actionpack (3.0.5) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
      actionpack (3.0.5) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
      railties (3.0.5) lib/rails/rack/logger.rb:13:in `call'
      rack (1.2.1) lib/rack/runtime.rb:17:in `call'
      activesupport (3.0.5) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
      rack (1.2.1) lib/rack/lock.rb:11:in `block in call'
      <internal:prelude>:10:in `synchronize'
      rack (1.2.1) lib/rack/lock.rb:11:in `call'
      actionpack (3.0.5) lib/action_dispatch/middleware/static.rb:30:in `call'
      railties (3.0.5) lib/rails/application.rb:168:in `call'
      railties (3.0.5) lib/rails/application.rb:77:in `method_missing'
      railties (3.0.5) lib/rails/rack/log_tailer.rb:14:in `call'
      rack (1.2.1) lib/rack/content_length.rb:13:in `call'
      rack (1.2.1) lib/rack/chunked.rb:15:in `call'
      rack (1.2.1) lib/rack/handler/mongrel.rb:67:in `process'
      mongrel (1.2.0.pre2) lib/mongrel.rb:165:in `block in process_client'
      mongrel (1.2.0.pre2) lib/mongrel.rb:164:in `each'
      mongrel (1.2.0.pre2) lib/mongrel.rb:164:in `process_client'
      mongrel (1.2.0.pre2) lib/mongrel.rb:291:in `block (2 levels) in run'

我的宝石:

        Using rake (0.8.7) 
        Using RedCloth (4.2.2) 
        Using abstract (1.0.0) 
        Using activesupport (3.0.5) 
        Using builder (2.1.2) 
        Using i18n (0.5.0) 
        Using activemodel (3.0.5) 
        Using erubis (2.6.6) 
        Using rack (1.2.1) 
        Using rack-mount (0.6.13) 
        Using rack-test (0.5.7) 
        Using tzinfo (0.3.24) 
        Using actionpack (3.0.5) 
        Using mime-types (1.16) 
        Using polyglot (0.3.1) 
        Using treetop (1.4.9) 
        Using mail (2.2.15) 
        Using actionmailer (3.0.5) 
        Using arel (2.0.9) 
        Using activerecord (3.0.5) 
        Using activeresource (3.0.5) 
        Using authlogic (2.1.6) 
        Using xml-simple (1.0.14) 
        Using aws-s3 (0.6.2) 
        Using block_helpers (0.3.3) 
        Using bundler (1.0.10) 
        Using diff-lcs (1.1.2) 
        Using json (1.4.6) 
        Using gherkin (2.3.4) 
        Using term-ansicolor (1.0.5) 
        Using cucumber (0.10.0) 
        Using cucumber-rails (0.3.2) 
        Using daemons (1.0.10) 
        Using database_cleaner (0.6.5) 
        Using factory_girl (1.3.3) 
        Using faker (0.9.5) 
        Using formtastic (1.2.3) 
        Using gem_plugin (0.2.3) 
        Using haml (3.0.25) 
        Using thor (0.14.6) 
        Using railties (3.0.5) 
        Using rails (3.0.5) 
        Using kaminari (0.10.4) 
        Using mongrel (1.2.0.pre2) 
        Using mysql2 (0.2.6) 
        Using nokogiri (1.4.4) 
        Using paperclip (2.3.8) 
        Using rspec-core (2.5.1) 
        Using rspec-expectations (2.5.0) 
        Using rspec-mocks (2.5.0) 
        Using rspec (2.5.0) 
        Using yard (0.6.4) 
        Using pickle (0.4.4) 
        Using populator (1.0.0) 
        Using rspec-rails (2.5.0) 
        Using webrat (0.7.3) 

您的应用程序是否使用'UTF-8'或'ASCII-8BIT'作为默认编码。另外,如果没有任何Flash消息,还会收到此错误吗?
Pan Thomakos 2011年


只是发表评论:我使用TextEdit编辑了一个文件,并将其==>'<==更改为==>`<==:然后抛出该错误。不要使用TextEdit一堆垃圾
Rambatino,2014年

Answers:


16

我怀疑您是将Haml模板的一部分复制/粘贴到了文件中,还是正在使用非Unicode /非UTF-8友好的编辑器。

看看是否可以在UTF-8友好编辑器中从头开始重新创建该文件。任何平台都有很多东西,看看是否能解决您的问题。首先使用删除行#content并手动重新键入。


感谢您的命令。我确实有一个无效的角色。我的文字中有一个'。这是非常困难的,因为错误行并不相同。感谢您的回答!
Michael Koper

63

我按照以下步骤解决了它:

  • 确保config.encoding = "utf-8"在application.rb文件中。
  • 确保您使用的是“ mysql2” gem。
  • 放在# encoding: utf-8包含UTF-8字符的文件的顶部。
  • <App Name>::Application.initialize!在environment.rb文件中的行上方,添加以下两行:

    Encoding.default_external = Encoding::UTF_8
    Encoding.default_internal = Encoding::UTF_8
    

http://rorguide.blogspot.com/2011/06/incompatible-character-encodings-ascii.html


16
使用mysql2对我来说很好,谢谢!-改变在适配器database.ymlmysql2的!
pex

2
我不得不使用宝石'mysql2', '<0.3'的建议在这里:stackoverflow.com/questions/5840742/...
marimaf

1
编辑config / database.yml,将适配器“ mysql”更改为“ mysql2”,然后可以使用!
Tim Wong

另请注意,Encoding.default_external = Encoding::UTF_8似乎不适用于该/lib文件夹中的ruby文件。# encoding: utf-8在每个文件的顶部都添加了技巧!
Augustin Riedinger

48

尝试找到导致此问题的确切行,然后执行UTF8编码,此解决方案对我有用。

title.to_s.force_encoding(“ UTF-8”)


8
即使在标准红宝石中似乎也有一些功能似乎引起了这种情况。对我来说是Digest :: MD5.digest(Random.rand(10000).to_s)
Smar

puts "debug URL is #{request.fullpath}"在控制器中从导轨4.2.8切换到4.2.9引起错误时,更改为request.fullpath.inspect即可解决该问题。
jpw

11

ASCII-8BIT是Ruby对正常0-0x7f ASCII字符集以上的字符的描述,它们是单字节字符。通常情况下,可能会类似于ISO-8859-1或其同类之一。

如果您可以确定是哪个字符导致了问题,那么可以告诉Ruby 1.9.2在该字符的字符集与UTF-8之间进行转换。

詹姆斯·格雷(James Gray)撰写了一系列博客,讨论了这类问题以及如何处理它们。我建议通过他们。

不兼容的字符编码:ASCII-8BIT和UTF-8

通常会发生这种情况,因为您尝试连接两个字符串,并且其中一个包含未映射到另一个字符串的字符集的字符。ISO-8859-1中的某些字符在UTF-8中没有等效字符,反之亦然,并且如何处理那些不兼容的字符串连接需要程序员介入。


2
Ruby对ISO-8859-1的描述是“ ISO-8859-1”。“ ASCII-8BIT”是ruby对二进制数据的描述。
sepp2k 2011年


7

对于Haml,请使用编码提示:

-# coding: UTF-8

在Haml页面的左上方。


5

我安装了gem软件包mysql2。

gem install mysql2

然后我在database.yml中的mysql2中更改了适配器。


是的,我摇滚了,它在这里确实有用!:D


3

为防止编码变量的错误“无法修改冻结的字符串”,可以使用:var.dup.force_encoding(Encoding::ASCII_8BIT)var.dup.force_encoding(Encoding::UTF_8)


2

我遇到过类似的问题。尽管我已经解决了UTF-8编码(使用mysql2和Encoding.default_external = Encoding :: UTF_8 ...)不兼容的字符编码:当我使用了错误的帮助程序参数时,UTF-8和ASCII-8BIT出现了,例如f.button :submit, "Zrušiť"-可以正常工作,但是f.button "Zrušiť"-抛出编码错误。


2

将应用程序从Ruby 1.8.7迁移到1.9.3时遇到错误,它仅在生产中出现。原来,我的Memcache存储区中有一些剩菜。我的应用程序现在编码敏感的Ruby 1.9.3版本尝试将旧的ASCII-8BIT值与新的UTF-8混合使用。

这就像刷新缓存为我修复它一样简单。


0

在自定义CoffeeScript文件上,我遇到了类似的问题。我通过将最终行编码从“ Unix / Linux”更改为“ Mac OS Classic”来解决了


0

pdf文档的创建rails-latex-gem导致了类似的问题。我通过修改layouts/application.pdf.erb为解决了

\begin{document}

<%= yield.force_encoding("UTF-8") %>


\end{document}

0

我遇到这个问题很奇怪,因为我忘记指定'type'参数。例如:

add_column :cms_push_msgs, :android_title

应该是:

add_column :cms_push_msgs, :android_content, :string 

0

仅作记录:对我而言,它原来是名为“ mysql”的宝石……显然,默认情况下,它与US-ASCII 8位一起使用。因此,将其更改为名为mysql2的gem (这里的2是重点)可以解决我的所有问题。

我看了上面发布的@ gem列表-Michael Koper显然已经安装了mysql2,但是我也发布了它,以防有人也有此问题..(花了我一些时间来弄清楚)。

如果您不喜欢此答案,请发表评论,我将其删除。

PS:德国变音符号(ä,ö和ü)用mysql搞砸了


请确保将Gemfile和database.yml中的mysql切换为mysql2 –atomiccoder,
2014年

0

在Ruby 1.9.2上解析在Ruby 1.8上正确解析的CSV文件时,我遇到了同样的问题。我在这里找到了答案。当使用Ruby CSV模块打开CSV文件时,必须指定如下的UTF-8:

CSV.foreach("file.txt", encoding: "UTF-8") do |row|
   # foo and bar correctly encoded
   foo, bar, ... = row
end

0

我遇到了类似的问题,宝石磨砂膏自动为我修复了它。https://github.com/hsbt/string-scrub 如果给定的字符串包含无效的字节序列,则将该无效的字节序列替换为unicode替换字符( ),并返回新的字符串。


0

我在最近的项目中从Rails 4.1,Ruby 2.3.3获得了相同的神秘错误消息,stacktrace源自布局application.html.haml

在大肆追赶鹅之后,罪魁祸首是最近被添加到所有页面页脚的UTF-8字符。由于某些奇怪的原因,错误只会间歇性地显示。

将UTF-8字符替换为相应的HTML转义序列可以&#xHHHH; 解决此问题。

我希望这可以节省其他人将来的时间。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.