Question

Trying to establish a connection from a module in Rails and get no connection to server. I have tested the same code outside Rails and it works fine.

require 'rubygems'
require 'net-ldap'

module Foo
  module Bar
    class User

    attr_reader :ldap_connection

    def initialize
      @ldap = Net::LDAP.new(:host => "<ip-number>", :port => 389)
      @treebase = "ou=People, dc=foo, dc=bar"
      username = "cn=Manager"
      password = "password"
      @ldap.auth username, password

      begin
        if @ldap.bind
          @ldap_connection = true
        else
          @ldap_connection = false
        end
        rescue Net::LDAP::LdapError
          @ldap_connection = false
        end
      end 
    end
  end
end

Getting Net::LDAP::LdapError: no connection to server exception.

Was it helpful?

Solution

I found a solution/workaround for my problem with auto-loading in Rails. Added a new initializer to ensure that all Ruby files under lib/ get required:

Added config/initializers/require_files_in_lib.rb with this code

Dir[Rails.root + 'lib/**/*.rb'].each do |file|
    require file
end

Read more about the workaround: Rails 3 library not loading until require

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top