Active Merchant - uninitialized constant ActiveSu

2019-05-07 02:58发布

I have activemerchant 1.16.0 and rails 3.0.5.

I am trying to build a basic code to communicate with PayPal's gateway using active merchant.

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

I get the following error:

/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)

This error propagates from the gateway.authorize() call. Any idea what's wrong with my setup? Thanks.

2条回答
放我归山
2楼-- · 2019-05-07 03:30

According to the question, it doesn't work when the code is by itself, but works when require "stringio" is added.

My suspicion is that ActiveMerchant is unit-tested, but for some reason the dependency on StringIO isn't detected by those unit tests, possibly because other parts of the unit testing code indirectly requires stringio.

One thing I recently found out was that require 'yaml' gives you the stringio library as a side effect:

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

and it wouldn't be hard to imagine unit tests for ActiveMerchant (or other parts of Rails) requiring yaml.

However, this is only speculation. I haven't checked, as I don't use Rails.

查看更多
干净又极端
3楼-- · 2019-05-07 03:32

Andrew Grimm pretty much hit the nail on the head with his original comment on this question. The missing require 'stringio' is indeed the issue. But it is a bug with Rails, more specifically ActiveSupport 3.0.9 (which is where the error seems to be coming from). We can trace it down using the git commit history of rails.

First we need to check out rails and switch to the v3.0.9 tag. If we now look at activesupport/lib/active_support/xml_mini/rexml.rb we see that require 'stringio' is not there. In and of itself this is not significant, but bear with me. We can now switch to the next tag (v3.0.10.rc1), and we'll see that the file hasn't been updated (it is likely that this version of rails will have the same issue). Next tag in line is v3.1.0.beta1, notice that this time around there is a require 'stringio' at the top of the file.

We can check out the commit that brought in this change (this one here from Jan 19th 2011). The commit message says:

fixed a missing require that causes trouble when using AS in a non-rails env

This would indicate that as long as you're in a rails environment this issue wouldn't surface. So, my guess is something about the environment caused the issue to come up, may have something to do with the fact that the OP says they are using rails 3.0.5, but the error is coming from activesupport-3.0.9. Perhaps the code was called from a rake task that forgot to inherit from :environment (difficult to say without more info). Either way, putting require 'stringio' at the top of the code is definitely the fix, until you can upgrade to Rails 3.1 (once it comes out) at which point the require will no longer be needed.

查看更多
登录 后发表回答