# Controller code and uninitialized constants

**URL:** <https://www.ruby-forum.com/t/controller-code-and-uninitialized-constants/182985>\
**Category:** Rails\
**Created:** [February 5, 2010, 5:21pm UTC](https://www.ruby-forum.com/t/controller-code-and-uninitialized-constants/182985 "2010-02-05T17:21:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 5, 2010, 5:21pm UTC](https://www.ruby-forum.com/t/controller-code-and-uninitialized-constants/182985/1 "2010-02-05T17:21:41Z")

</div>

YM4R\_GM plugin…

This controller code works but clearly has limitations…

map\_1 = GMarker.new([@salons[0].lat, @salons[0].lng])  
@map.overlay\_init(map\_1)

but I really need this in a loop…

```
for salon in @salons do
  map_id += 1
  map_id_ = "map_id_" + map_id.to_s
  map_id = Gmarker.new([salon.lat, salon.lng])
  @map.overlay_init(map_id)
end

```

But inside the loop, I get the following error that I can’t seem to  
figure out a way to prevent…

NameError (uninitialized constant SalonsController::Gmarker):

How to use the Gmarker method inside a loop?

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 5, 2010, 7:19pm UTC](https://www.ruby-forum.com/t/controller-code-and-uninitialized-constants/182985/2 "2010-02-05T19:19:21Z")

</div>

On Feb 5, 11:19 am, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> map_id += 1
> 
> ```
> 
> How to use the Gmarker method inside a loop?
> 
> Craig
> 
> –  
> This message has been scanned for viruses and  
> dangerous content by MailScanner, and is  
> believed to be clean.

Ruby is case-sensitive. And that loop is rather sketchy… what you’re  
doing can just be replaced with something like

@salons.each {|salon| @map.overlay\_init GMarker.new([salon.lat,  
salon.lng])}

If you end up actually needing the array indices, each\_with\_index will  
do nicely.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 6, 2010, 1:13am UTC](https://www.ruby-forum.com/t/controller-code-and-uninitialized-constants/182985/3 "2010-02-06T01:13:29Z")

</div>

On Fri, 2010-02-05 at 10:17 -0800, pharrington wrote:

> > ```
> > for salon in @salons do
> > 
> > ```
> > 
> > How to use the Gmarker method inside a loop?

> Ruby is case-sensitive. And that loop is rather sketchy… what you’re  
> doing can just be replaced with something like
> 
> @salons.each {|salon| @map.overlay\_init GMarker.new([salon.lat,  
> salon.lng])}
> 
> If you end up actually needing the array indices, each\_with\_index will  
> do nicely.

* * *

friggin awesome - thanks

loop was sketchy because I myself was getting loopy after trying so many  
variations…

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 6, 2010, 4:18pm UTC](https://www.ruby-forum.com/t/controller-code-and-uninitialized-constants/182985/4 "2010-02-06T16:18:12Z")

</div>

I don’t think you’re going to like this. I think the issue is that in  
the for loop you need to capitalize the m in Gmarker (as in your  
original code).

Also, ruby offers something called each with index. Your code could  
become something like

@salons.each\_with\_index |salon, map\_id|  
map\_id\_ = “map\_id\_” + map\_id.to\_s  
map\_id = GMarker.new([salon.lat, salon.lng])  
@map.overlay\_init(map\_id)  
end

Hope that helps.

Luke

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 6, 2010, 6:43pm UTC](https://www.ruby-forum.com/t/controller-code-and-uninitialized-constants/182985/5 "2010-02-06T18:43:21Z")

</div>

On Sat, 2010-02-06 at 07:16 -0800, Luke C. wrote:

> ```
> @map.overlay_init(map_id)
> 
> ```
> 
> end
> 
> Hope that helps.

* * *

duh - I missed the Gmarker/GMarker issue. That clearly was the problem  
but pharrington gave me some very concise/clever way of handling it  
which worked beautifully. Thanks

Craig

–  
This message has been scanned for viruses and  
dangerous content by MailScanner, and is  
believed to be clean.
