在 Ruby 1.8.7 中使用 ActionMailer 时有什么方法可以避免编码错误
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能在使用 Ruby 1.8 和使用 gem 定义的常量编码时遇到问题。
例如,我们发现一个在全局范围内包含 REXML 的类存在问题。
库中有问题的行是:
This is 检查一个名为 Encoding 的全局常量。 (可以在全局范围内的任何 gem 中定义。)我们的问题实际上是包含,然后使 REXML::Encoding 在任何地方都可以作为 Encoding 使用。您可以尝试 grep 或确认代码库的“模块编码”或“类编码”。
希望这有帮助。
下面的堆栈跟踪定义了问题。
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:
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.
在尝试测试我的设备确认电子邮件时,我遇到了同样的错误。奇怪的是,单独运行测试并没有抛出这个异常。运行整个堆栈就可以了。在进行二进制搜索后,我发现我的规范之一使用 REXML 来解析一些内容。
我不知道为什么我包含 REXML,大概是因为我想将 REXML:: 保存在 Document 前面。但是将代码更改为此解决了问题:
如果您碰巧在某处使用这样的代码,这可能会修复此编码! 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.
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:
if you happen to use code like this somewhere, this may fix this encode! issue with ruby 1.8.7
我发现问题出在邮件程序 gem 上,更改为 2.2.13 为我修复了它。
I found the issue to be with the mailer gem, changing to 2.2.13 fixed it for me.