2013-02-27 55 views
2

尝试从Rails中的模块建立连接并且未连接到服务器。我已经测试了Rails之外的相同代码,它工作正常。Ldap gem在Rails中抛出与服务器异常的连接

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 

得到Net::LDAP::LdapError: no connection to server异常。

回答

1

我发现了一个解决方案/解决方法,用于在Rails中自动加载的问题。增加了一个新的初始化,以确保lib下的所有Ruby文件/获取必需的:

新增config/initializers/require_files_in_lib.rb使用此代码

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

了解更多关于变通方法:Rails 3 library not loading until require

相关问题