# Hash problem any ideas?

**URL:** <https://www.ruby-forum.com/t/hash-problem-any-ideas/128900>\
**Category:** Rails\
**Created:** [February 12, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/hash-problem-any-ideas/128900 "2008-02-12T15:06:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 12, 2008, 3:06pm UTC](https://www.ruby-forum.com/t/hash-problem-any-ideas/128900/1 "2008-02-12T15:06:53Z")

</div>

on controller i have this:

ldaphash = {}

ldap.search( :base =\> treebase, :filter =\> filter ) do |entry|

email = entry[:mail].to\_s  
uid = entry[:uid].to\_s  
fullname = entry[:cn].to\_s  
fullname = Iconv.iconv(“ISO-8859-1”, “UTF-8”, fullname)  
@fullname = fullname

if email != ‘’ && uid != ‘’ then  
#render :text =\> “#{fullname} (#{email})”  
ldaphash[“name”] = “#{fullname}”  
ldaphash[“email”] = “#{email}”  
ldaphash[“uid”] = “#{uid}”  
end  
end

now on the view page I try to access that hash.

\<% ldaphash.each\_pair do {key,val} %\>  
\<%= “#{key} =\> #{val}” %\>  
\<% end %\>

Since I am total noob with these hashes on ruby any ideas ?

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 12, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/hash-problem-any-ideas/128900/2 "2008-02-12T15:18:44Z")

</div>

On 12 Feb 2008, at 14:06, KTU wrote:

> fullname = Iconv.iconv(“ISO-8859-1”, “UTF-8”, fullname)  
> now on the view page I try to access that hash.
> 
> \<% ldaphash.each\_pair do {key,val} %\>  
> \<%= “#{key} =\> #{val}” %\>  
> \<% end %\>
> 
> Since I am total noob with these hashes on ruby any ideas ?  
> If you want something in your controller to magically appear in your  
> views you need to be using instance variables (ie @ldaphash). And the  
> last bit should of course be  
> \<% ldaphash.each\_pair do |key,val| %\>  
> \<%= “#{key} =\> #{val}” %\>  
> \<% end %\>

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 12, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/hash-problem-any-ideas/128900/3 "2008-02-12T15:28:45Z")

</div>

ok i did this on controller:

@ldaphash = ldaphash

and on view i did added this one:

\<% ldaphash.each\_pair do |key,val| %\>  
\<%= “#{key} =\> #{val}” %\>  
\<% end %\>

now I get error:

undefined local variable or method `ldaphash’ for #\<ActionView::Base:  
0xb76bd0c0\>

On Feb 12, 4:17 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 12, 2008, 3:48pm UTC](https://www.ruby-forum.com/t/hash-problem-any-ideas/128900/4 "2008-02-12T15:48:20Z")

</div>

ok now i get error

You have a nil object when you didn’t expect it!

view is now

\<% @ldaphash.each\_pair do |key,val| %\>  
\<%= “#{key} =\> #{val}” %\>  
\<% end %\>

If I create simple file like this test.rb

ldaphash = { }

ldap.search( :base =\> treebase, :filter =\> filter ) do |entry|

email = entry[:mail].to\_s  
uid = entry[:uid].to\_s  
fullname = entry[:cn].to\_s  
fullname = Iconv.iconv(“ISO-8859-1”, “UTF-8”, fullname)  
@fullname = fullname

if email != ‘’ && uid != ‘’ then  
ldaphash[“name”] = “#{fullname}”  
ldaphash[“email”] = “#{email}”  
ldaphash[“uid”] = “#{uid}”  
end

ldaphash.each\_pair do |key, val|  
puts “#{key} =\> #{val}”  
end

and run it then it works on command line but not on the web ?

On Feb 12, 4:36 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 12, 2008, 3:55pm UTC](https://www.ruby-forum.com/t/hash-problem-any-ideas/128900/5 "2008-02-12T15:55:25Z")

</div>

ok now I got something to print but only the last item on the hash ??  
weird … any clues ?

---

<div class="post-metadata">

**Author:** ![KTU](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/ecae2f/32.png) [@KTU](https://www.ruby-forum.com/u/KTU)\
**Post date:** [February 12, 2008, 3:37pm UTC](https://www.ruby-forum.com/t/hash-problem-any-ideas/128900/6 "2008-02-12T15:37:05Z")

</div>

On 12 Feb 2008, at 14:27, KTU wrote:

> now I get error:
> 
> undefined local variable or method `ldaphash’ for #\<ActionView::Base:  
> 0xb76bd0c0\>  
> It’s still an instance variable in the view, so it needs to be  
> @ldaphash.each\_pair
