ruby-ldap gem not work in rails3 app, but work in rails console

狂风中的少年 提交于 2019-12-11 11:18:28

问题


I want to build a rails3 website authed with LDAP, so I chose ruby-ldap gem (not net/ldap) which we used in our old rails2 apps and works very well.

But I keep on getting weird error in rails3 app, See the codes below:

require 'ldap'
class WelcomeController < ApplicationController

  def index  
    begin
      @test = LDAP::Conn.new('10.72.64.11', 389)
    rescue LDAP::Error
      p LDAP::Error
    end  
    render :text => "ok"
  end
end 

welcome#index is my root route. Most time, the app crashes when going to LDAP::Conn.new('10.72.64.11', 389), even I tried to use "pry" to debug and track, throwing

[1]    24797 trace trap  rails s

and the WEBrick server will be terminated right that time.

Sometimes it throws another type error when I use "pry" to step, #<NameError: uninitialized constant WelcomeController::LDAP>

While try it in the console, everything goes well.

1.9.3-p194 :001 > require 'ldap'
 => true 
1.9.3-p194 :002 > @test = LDAP::Conn.new('10.72.64.11', 389)
 => #<LDAP::Conn:0x00000101289568> 
1.9.3-p194 :003 > 

Can you guide me out of this crazy stuff? I am using ruby 1.9.3p194 and rails 3.2.8


回答1:


A few months later, I kind of figure out what the problem is...

The ruby-ldap gem has problem on running on the rails default server: Webrick.

Try Pow or Passenger, it works perfect!

After reading this page: http://www.ruby-forum.com/topic/62920

I tried moving the require 'ldap' from the controller or model file, and into the very top line of my environment file (xxxlocal.rb)

After I did that, I was able to run it ok in webrick also.



来源:https://stackoverflow.com/questions/11979920/ruby-ldap-gem-not-work-in-rails3-app-but-work-in-rails-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!