在 Ruby 1.8.7 中使用 ActionMailer 时有什么方法可以避免编码错误

发布于 2024-11-17 16:28:44 字数 664 浏览 3 评论 0原文

在 Rails 中通过 ActionMailer 发送简单的电子邮件时,我不断收到以下错误:

NoMethodError: undefined method `encode!' for "Hello":String

每当运行以下命令时都会触发此错误:

def hello_world_email()

   mail( :from => "me",
      :to => "you,
      :subject => "Hello World"
       )
end

通过研究,它看起来像是由 Ruby 1.8.7 和 1.9.* 之间的差异引起的,其中具有内置的字符编码支持。

有没有办法让 ActionMailer 与 Ruby 1.8.7 一起工作并避免这个问题? (我可以在我的机器上升级 Ruby,但我不能对参与该项目的其他人做同样的事情)。鉴于整个 Rails 3.0.9(ActionMailer 是其中的一部分)都旨在与 Ruby 1.8.7 一起使用,我一直认为一定有一种方法......

注意:我的 Ruby 确切版本是 ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0] (预装在 OSX 上)。

I keep getting the following error when sending a simple e-mail via ActionMailer in Rails:

NoMethodError: undefined method `encode!' for "Hello":String

This is triggered whenever the following is run:

def hello_world_email()

   mail( :from => "me",
      :to => "you,
      :subject => "Hello World"
       )
end

From researching this, it looks like it's caused by the differences between Ruby 1.8.7 and 1.9.*, which has built-in character encoding support.

Is there any way of getting ActionMailer to work with Ruby 1.8.7 and avoid this issue? (I can upgrade Ruby on my machine, but I can't do the same for everyone else working on the project). Given that the whole of Rails 3.0.9 (of which ActionMailer is a part) is meant to work with Ruby 1.8.7, I keep thinking that there must be a way...

Note: My exact version of Ruby is ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0] (pre-installed on OSX).

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

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

发布评论

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

评论(3

我不在是我 2024-11-24 16:28:44

您可能在使用 Ruby 1.8 和使用 gem 定义的常量编码时遇到问题。
例如,我们发现一个在全局范围内包含 REXML 的类存在问题。

库中有问题的行是:

value.encode!(charset) if defined?(Encoding) && charset

This is 检查一个名为 Encoding 的全局常量。 (可以在全局范围内的任何 gem 中定义。)我们的问题实际上是包含,然后使 REXML::Encoding 在任何地方都可以作为 Encoding 使用。您可以尝试 grep 或确认代码库的“模块编码”或“类编码”。

希望这有帮助。

下面的堆栈跟踪定义了问题。

NoMethodError: undefined method `encode!' for "Generate":String
mail (2.2.19) lib/mail/fields/unstructured_field.rb:169:in `encode'
mail (2.2.19) lib/mail/fields/unstructured_field.rb:138:in `fold'
mail (2.2.19) lib/mail/fields/unstructured_field.rb:108:in `wrapped_value'
mail (2.2.19) lib/mail/fields/unstructured_field.rb:70:in `do_encode'
mail (2.2.19) lib/mail/fields/unstructured_field.rb:52:in `encoded'
mail (2.2.19) lib/mail/field.rb:123:in `send'
mail (2.2.19) lib/mail/field.rb:123:in `method_missing'
mail (2.2.19) lib/mail/header.rb:190:in `encoded'
mail (2.2.19) lib/mail/header.rb:189:in `each'
mail (2.2.19) lib/mail/header.rb:189:in `encoded'
mail (2.2.19) lib/mail/message.rb:1708:in `encoded'
actionmailer (3.0.10) lib/action_mailer/base.rb:445:in `set_payload_for_mail'
actionmailer (3.0.10) lib/action_mailer/base.rb:425:in `deliver_mail'
activesupport (3.0.10) lib/active_support/notifications.rb:52:in `instrument'
activesupport (3.0.10) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.10) lib/active_support/notifications.rb:52:in `instrument'
actionmailer (3.0.10) lib/action_mailer/base.rb:424:in `deliver_mail'
mail (2.2.19) lib/mail/message.rb:230:in `deliver'

You may have an issue with using Ruby 1.8 and having a constant Encoding defined by a gem.
For example we found an issue with a class that was including REXML at a global scope.

The line in question in the library was:

value.encode!(charset) if defined?(Encoding) && charset

This is checking for a global constant called Encoding. (Which could be defined in any gem at global scope.) Our problem was actually the include which then made REXML::Encoding available everywhere as Encoding. You could try grepping or acking your codebase for "module Encoding" or "class Encoding".

Hope this helps.

The stack trace below defines the problem.

NoMethodError: undefined method `encode!' for "Generate":String
mail (2.2.19) lib/mail/fields/unstructured_field.rb:169:in `encode'
mail (2.2.19) lib/mail/fields/unstructured_field.rb:138:in `fold'
mail (2.2.19) lib/mail/fields/unstructured_field.rb:108:in `wrapped_value'
mail (2.2.19) lib/mail/fields/unstructured_field.rb:70:in `do_encode'
mail (2.2.19) lib/mail/fields/unstructured_field.rb:52:in `encoded'
mail (2.2.19) lib/mail/field.rb:123:in `send'
mail (2.2.19) lib/mail/field.rb:123:in `method_missing'
mail (2.2.19) lib/mail/header.rb:190:in `encoded'
mail (2.2.19) lib/mail/header.rb:189:in `each'
mail (2.2.19) lib/mail/header.rb:189:in `encoded'
mail (2.2.19) lib/mail/message.rb:1708:in `encoded'
actionmailer (3.0.10) lib/action_mailer/base.rb:445:in `set_payload_for_mail'
actionmailer (3.0.10) lib/action_mailer/base.rb:425:in `deliver_mail'
activesupport (3.0.10) lib/active_support/notifications.rb:52:in `instrument'
activesupport (3.0.10) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.10) lib/active_support/notifications.rb:52:in `instrument'
actionmailer (3.0.10) lib/action_mailer/base.rb:424:in `deliver_mail'
mail (2.2.19) lib/mail/message.rb:230:in `deliver'
妥活 2024-11-24 16:28:44

在尝试测试我的设备确认电子邮件时,我遇到了同样的错误。奇怪的是,单独运行测试并没有抛出这个异常。运行整个堆栈就可以了。在进行二进制搜索后,我发现我的规范之一使用 REXML 来解析一些内容。

    require 'spec_helper'
    require 'rexml/document'
    include REXML
    doc = Document.new(response.body)

我不知道为什么我包含 REXML,大概是因为我想将 REXML:: 保存在 Document 前面。但是将代码更改为此解决了问题:

    require 'spec_helper'
    require 'rexml/document'
    doc = REXML::Document.new(response.body)

如果您碰巧在某处使用这样的代码,这可能会修复此编码! ruby 1.8.7 的问题

I had the same error, when trying to test my devise confirmation emails. Weird thing was, running the test isolated did not throw this exception. Running the whole stack did. After doing a binary search, i found, that one of my specs used REXML to parse some content.

    require 'spec_helper'
    require 'rexml/document'
    include REXML
    doc = Document.new(response.body)

I dont't know, why i included REXML, presumably, because i wanted to save the REXML:: in front of Document. But changing the code to this fixed the problem:

    require 'spec_helper'
    require 'rexml/document'
    doc = REXML::Document.new(response.body)

if you happen to use code like this somewhere, this may fix this encode! issue with ruby 1.8.7

岁月染过的梦 2024-11-24 16:28:44

我发现问题出在邮件程序 gem 上,更改为 2.2.13 为我修复了它。

I found the issue to be with the mailer gem, changing to 2.2.13 fixed it for me.

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