Rails 3 编码::兼容性错误

发布于 2024-10-01 05:35:32 字数 303 浏览 0 评论 0原文

我正在开发一个通过 ajax 提交法语翻译的 Rails 应用程序,由于某种原因,我不断在日志中收到以下错误:

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

有谁知道如何解决这个问题?

修复:这适用于 WEBrick 服务器

#encode: UTF-8 放置在您想要使用不同字符的每个文件的顶部,

我无法让它在带有 Thin 的 Rails 服务器上工作。 ..还有其他人遇到这个吗?

I am working on a rails app that submits a french translation via ajax and for some reason I keep getting the following error in the log:

Encoding::CompatibilityError incompatible character encodings: UTF-8 and ASCII-8BIT

Does anyone know how to fix this?

FIX:This works on the WEBrick sever

Place # encode: UTF-8 at the top of each file you want to work with different chars

I can't get this to work on a rails server with Thin... anyone else run into this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

删除会话 2024-10-08 05:35:32

https://rails.lighthouseapp.com/projects/8994/tickets/4336-ruby19-subscribed-string-form-parameters-with-non-ascii-characters-cause-encoding-errors

以上链接解决了我的问题。

具体来说,在发送字符串进行翻译之前对其进行 myString.force_encoding('UTF-8') 处理。

将示例代码放入 Application_controller.rb 文件中,一切顺利

https://rails.lighthouseapp.com/projects/8994/tickets/4336-ruby19-submitted-string-form-parameters-with-non-ascii-characters-cause-encoding-errors

the above link fixed my problem.

Specifically myString.force_encoding('UTF-8') on the string before sending it for translation.

Placed the sample code in the Application_controller.rb file and all is well

城歌 2024-10-08 05:35:32

我知道这已经很旧了,但我遇到了同样的问题,解决方案位于@dennismonsewicz 给出的链接中。详细来说,代码是:

是:

before_filter :force_utf8_params

  def force_utf8_params
    traverse = lambda do |object, block|
      if object.kind_of?(Hash)
        object.each_value { |o| traverse.call(o, block) }
      elsif object.kind_of?(Array)
        object.each { |o| traverse.call(o, block) }
      else
        block.call(object)
      end
      object
    end
    force_encoding = lambda do |o|
      o.force_encoding(Encoding::UTF_8) if o.respond_to?(:force_encoding)
    end
    traverse.call(params, force_encoding)
  end

I know this is old, but I had the same problem and the solution was in the link @dennismonsewicz gave. In detail, the code was:

was:

before_filter :force_utf8_params

  def force_utf8_params
    traverse = lambda do |object, block|
      if object.kind_of?(Hash)
        object.each_value { |o| traverse.call(o, block) }
      elsif object.kind_of?(Array)
        object.each { |o| traverse.call(o, block) }
      else
        block.call(object)
      end
      object
    end
    force_encoding = lambda do |o|
      o.force_encoding(Encoding::UTF_8) if o.respond_to?(:force_encoding)
    end
    traverse.call(params, force_encoding)
  end
小清晰的声音 2024-10-08 05:35:32

我通过将 utf8 文件转换为 ascii 解决了这个问题。
请参阅此处的答案:ruby 1.9 + sinatra 不兼容的字符编码:ASCII-8BIT 和 UTF-8

I fixed this issue by converting an utf8 file to ascii.
See the answer here: ruby 1.9 + sinatra incompatible character encodings: ASCII-8BIT and UTF-8

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文