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

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

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.

Related

Why does the hash method return a different value in rails console?

In my app
<%= "a_string".hash %>
renders as 4318227885144361583
in rails console:
$ rails console
Loading development environment (Rails 3.2.3)
1.9.2p290 :001 > "a_string".hash
=> -917414088101530508
Same machine, same rails app, same environment, same version of ruby.
Why does it get different values?
This has nothing to do with Rails.
Fire up IRB again and see what you get across sessions:
1.8.7 :003 > "a_string".hash
=> -1300030395
1.8.7 :004 > exit
$ irb
1.8.7 :001 > "a_string".hash
=> 1520614759
1.8.7 :002 > exit
$ irb
1.8.7 :001 > "a_string".hash
=> 1991940479
http://www.ruby-doc.org/core-1.9.3/String.html#method-i-hash
I would offer up, that most likely you want to do something like this?
require 'digest/md5'
Digest::MD5.hexdigest("a_string")
=> "7a0d3f5c88466513b32cee16e0620162"
That will always return the same result.
Ruby 1.9 has always had this behaviour: part of the information that goes into the hashing algorithm is randomly picked on a per process basis
Versions of ruby 1.8.7 up to p352 behaved the way you expected: the hash of a string depended only on the contents of the string.
Unfortunately this opened the door for a type of denial of service attack: you could choose a large number of parameters that all hashed to the same value and would as such invoke pathological behaviour in the ruby Hash class: instead of O(1) access times you'd get O(n). This was fixed in ruby 1.8.7p357. There are more details in this post to the ruby talk list.

How can Rails automatically rescue from ActiveRecord::RecordNotFound in development mode?

I am using the following piece of code on my ApplicationController :
rescue_from ActiveRecord::RecordNotFound, :with => :not_found
def not_found
render :nothing => true, :status => :not_found
end
in order make Rails respond with correct status code and not raise an exception
in development mode.
I know that how-to-handle-errors-like-404-500-in-rails3 explains how Rails works in production mode. In other words, what I am trying to do in development mode is done without any piece of code in production mode.
How can I make development mode behave like production mode and get rid of the above piece of code?
Note that I am using Rails 3.2.3
Perhaps you want to turn this off?:
config.consider_all_requests_local = false
This is set to true in config/environments/development.rb by default.

Rails Active Admin resource problem

I recently have watched railscast 284 about active admin and wanted to implement it into my web app, however I am running into an issue when I add a resource. I get the following message every time I try to navigate to the created tab:
NameError in Admin::LoadsController#index
undefined local variable or method `per' for []:ActiveRecord::Relation
Rails.root: /Users/thomascioppettini/rails_projects/want-freight
Application Trace | Framework Trace | Full Trace
Request
Parameters:
{"order"=>"id_desc"}
Show session dump
Show env dump
Response
Headers:
None
The only thing I can think of that may affect the application is adding a recaptcha to devise, which active admin depends on.
For me, it looks like this is a pagination problem. What gem are you using? You should give as more details about your settup. Can you show us your resource file from admin directory? What version of rails and what ActiveAdmin are you using ?
If you are using the will_paginate gem, set the version to 3.0.pre2. I was using ~>3.0.pre2, which auto-updated to 3.0.2 when I ran a bundle update Reverting fixed the issue. If you're using Bundler, the line is this:
gem "will_paginate", "3.0.pre2"
I agree with Dawaid. It is a pagiantion error. Add "Kaminari" gem to you Gemfile. According to active admin docs, it is using kaminari for pagination.. will_paginate will also work for you as swilliams described...
As I understand active_admin doesn't support will_paginate anymore. But if you don't want to rewrite your pagination to Kaminari you can fix this problem with putting some code to initializers
# config/initializers/will_paginate.rb
if defined?(WillPaginate)
module WillPaginate
module ActiveRecord
module RelationMethods
alias_method :per, :per_page
alias_method :num_pages, :total_pages
end
end
end
end
module ActiveRecord
class Relation
alias_method :total_count, :count
end
end

Rails 3 + FactoryGirl: NameError: uninitialized constant Factory

ruby-1.9.2-p180 :007 > Factory.define :user do |user|
ruby-1.9.2-p180 :008 > user.email "user#example.com"
ruby-1.9.2-p180 :009?> user.password "foobar"
ruby-1.9.2-p180 :010?> user.password_confirmation "foobar"
ruby-1.9.2-p180 :011?> end
NameError: uninitialized constant Factory
My Gemfile:
group :test do
gem "rspec-rails"
gem 'webrat', '0.7.1'
gem 'spork', '0.9.0.rc4'
gem 'factory_girl_rails'
end
Even tough it seems I have everything as it should, I keep getting that error. I also have factories.rb created.
Thanks
I suppose you try in console in development environment. But you add the Factory gem only in test environment.
If you want access to Factory_girl in development use in your Gemfile :
group :test, :development do
gem 'factory_girl_rails'
end
Or if you want test your Factory launch your console in test environment :
rails c test
We had a similar problem on our end, rake spec seemed to be randomly failing with the error uninitialized constant FactoryGirl. The error was random -> coming and going. We went back half a dozen git commits to try and resolve it. In the end it was a silly mistake.
The fundamental problem is RAILS_ENV is set to development. It needs to be set to test when you run rake spec.
Address by:
Making certain that we are running rake spec in the RAILS_ENV test environment and its exported/sourced properly. To never confuse our environemnts, we modified zsh $RPROMPT env variable to show the current env.
export RPROMPT="[%{$fg_no_bold[yellow]%}$RAILS_ENV%{$reset_color%}]"
Require FactoryGirl in the spec ruby files gave a much better error message. At least rspec would run vs just fail outright this way when the environment was wrong. We also updated our gemfile to make sure factory_girl_rails and factory_girl were loaded both for development and testing.
Now we just run rpsec using the gem guard in a dedicated terminal with the proper RAILS_ENV set.
Its one of those gotchas.
We're running Ruby 1.9.3, Rails 3.2.9, Rspec 2.12, factory_girl 4.1.
I also ran into this while I was doing the Hartl tutorial.
If you are using "Spork" to speed up your tests and it is running in the background when you add the FactoryGirl code, you will need to stop it and restart it.
You should also change Factory.define to FactoryGirl.define, like in this example
FactoryGirl.define do
factory :user do
name 'John Doe'
date_of_birth { 21.years.ago }
end
end
from the factory_girl documentation

Webmock gem in rails 3 and properly including it

I'm likely doing something very simply wrong, but I'm not quite sure what it is. I am porting a rails 2 application to rails 3. This application uses webmock for a bunch of it's tests.
If I include
gem 'webmock'
In my Gemfile, the tests pass, but when I start the server and run the app locally, hitting a controller that should make a web call throws an error:
WebMock::NetConnectNotAllowedError
If I do NOT include the line in my Gemfile, then when I run the app locally, it works fine, but the tests error out with:
`require': no such file to load -- webmock (LoadError)
When this line is hit in my test_helper.rb
require 'webmock'
I'm guessing I've got something configured wrong, but I haven't hit the right google incantation to shed any light on it yet. Where I did I go astray?
Thank you.
Try telling your Gemfile to only load webmock when you're in a test environment:
group :test do
gem "webmock"
end
On my Ruby 1.9 Rails 3 instance I have something like the following:
group :test do
gem "mocha"
gem "webmock"
end
group :development do
gem 'ruby-debug19', :require => 'ruby-debug'
end