2013-02-27 42 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

相關問題