Soap4r and Rails

Published on 18 April 2007 by James in Ruby

8

I recently wrapped up a short project bridging a Rails application to the Lyris ListManager API. One of things I had to do was use soap4r to support client calls over SOAP to the ListManager services. I the midst of the development, I encountered the following problem:

NameError: uninitialized constant SOAP::Mapping::EncodedRegistry

While I found some support for the issue in a few places, the end result was that i needed to modify boot.rb for my Rails app to load the soap4r gem earlier in the boot process (sounds like a Java CLASSPATH issue, eh?). The problem is that the project installs Rails using a svn external link inside the vendor directory. The suggested fix didn’t quite cover what I needed though the spirit of fix was there. Here is what I changed in boot.rb to make it work:

From:

if File.directory?(“#{RAILS_ROOT}/vendor/rails”)
  require “#{RAILS_ROOT}/vendor/rails/railties/lib/initializer”

To:

if File.directory?(“#{RAILS_ROOT}/vendor/rails”)
# Making a change here to force the soap4r gem to preload before the SOAP version packaged with Ruby/Rails
  require ‘rubygems’
  begin
    require_gem ’soap4r’
  rescue => e
    raise “The gem soap4r was not found. Please perform a ‘gem install soap4r’.”
  end
# Now, back to our regularly scheduled program, already in progress…
require “#{RAILS_ROOT}/vendor/rails/railties/lib/initializer”

I hope this helps others that struggle over the same problem and are using the same project structure.

[tags]ruby on rails, soap, lyris[/tags]

8 Responses to “Soap4r and Rails”

  1. Ryan says:

    Would this problem not be the case if Rails were loaded from gems rather than from the vendors directory?

  2. James says:

    Ryan,

    No, it doesn’t matter which way you choose to launch Rails – via gems or under a vendor directory – the issue still remains.

    My post identifies the fact that the fix commonly found via Google only works if you are using Rails via gems. The fix I documented above solves the problem if Rails is loaded under the vendor directory – a fact that I found after spending some time wondering why the original fix didn’t work.

  3. Ryan says:

    I wonder if it would be possible to load soap4r from a plugin instead of a gem. Thought that might not solve the load order problem.

  4. Bob says:

    I’m trying your fix, but I still get the same error, when running inside rails. Standalone works fine. Any guesses?

    Thanks!

  5. James says:

    Bob,

    Without more information it may be hard to determine the problem. Just make sure that you have read all of the links that I referenced in my post, and be sure that you apply my fix only if you are running rails from under vendor, rather than as a gem.

  6. [...] Soap4r and Rails | Blue Jazz Consulting – Austin-based Ruby on Rails, Architecture, and Technical Le… (tags: soap rails) [...]

  7. Ben says:

    To get this to work in Rails 2.1 (and probably later versions) all I had to do was

    config.gem ’soap4r’, :lib => false

    in my environment.rb. No finagling of boot.rb required.

  8. James says:

    Thanks Ben!

Leave a Reply