Active Merchant – 未初始化的常量ActiveSupport :: XmlMini_REXML :: StringIO

我有activemerchant 1.16.0和rails 3.0.5。

我正在尝试构建一个基本代码,以便使用活动商家与PayPal的网关进行通信。

if credit_card.valid? # or gateway.purchase to do both authorize and capture response = gateway.authorize(1000, credit_card, :ip => "127.0.0.1") if response.success? gateway.capture(1000, response.authorization) puts "Purchase complete!" else puts "Error: #{response.message}" end else puts "Error: credit card is not valid. #{credit_card.errors.full_messages.join('. ')}" end 

我收到以下错误:

 /Library/Ruby/Gems/1.8/gems/activesupport-3.0.9/lib/active_support/xml_mini/rexml.rb:20:in `parse': uninitialized constant ActiveSupport::XmlMini_REXML::StringIO (NameError) 

此错误从gateway.authorize()调用传播。 知道我的设置有什么问题吗? 谢谢。

根据这个问题,它在代码本身时require "stringio" ,但在require "stringio"添加require "stringio"require "stringio"

我怀疑ActiveMerchant是经过unit testing的,但由于某些原因,这些unit testing没有检测到对StringIO的依赖,可能是因为unit testing代码的其他部分间接require s stringio。

我最近发现的一件事是require 'yaml'给你stringio库作为副作用:

 StringIO.new # NameError: uninitialized constant StringIO # from (irb):1 require "yaml" # => true StringIO.new # => # RUBY_VERSION # => "1.8.7" 

并且不难想象ActiveMerchant(或Rails的其他部分)的unit testing需要yaml。

但是,这只是猜测。 我没有检查,因为我不使用Rails。

安德鲁·格里姆(Andrew Grimm)对这个问题的原始评论几乎让人头疼。 缺少require 'stringio'确实是问题。 但它是Rails的一个错误,更具体地说是ActiveSupport 3.0.9(这似乎是错误的来源)。 我们可以使用rails的git commit history跟踪它。

首先,我们需要检查rails并切换到v3.0.9标签。 如果我们现在查看activesupport/lib/active_support/xml_mini/rexml.rb我们发现require 'stringio'不存在。 这本身并不重要,但请耐心等待。 我们现在可以切换到下一个标签(v3.0.10.rc1),我们将看到该文件尚未更新(这个版本的rails很可能会出现同样的问题)。 行中的下一个标记是v3.1.0.beta1,注意这次在文件顶部有一个require 'stringio'

我们可以查看带来此更改的提交( 此处自2011年1月19日起)。 提交消息说:

修复了在非rails env中使用AS时导致故障的缺失需求

这表明只要你处于铁轨环境中,这个问题就不会浮出水面。 因此,我的猜测是关于环境导致问题出现的问题,可能与OP表示他们使用的是rails 3.0.5这一事实有关,但错误来自activesupport- 3.0.9 。 也许代码是从忘记inheritance的rake任务调用的:environment (没有更多信息很难说)。 无论哪种方式,在代码顶部放置require 'stringio'肯定是修复,直到你可以升级到Rails 3.1(一旦它出来),此时将不再需要require。

Interesting Posts