尝试从 Rails 中的模块建立连接,但没有连接到服务器。我在 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
异常。
最佳答案
我找到了解决 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
关于ruby - Ldap gem 在 Rails 中抛出与服务器异常的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15121413/