# Google map to\_html not working

**URL:** <https://www.ruby-forum.com/t/google-map-to-html-not-working/76995>\
**Category:** Rails\
**Created:** [November 23, 2006, 8:18am UTC](https://www.ruby-forum.com/t/google-map-to-html-not-working/76995 "2006-11-23T08:18:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacquie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jacquie](https://www.ruby-forum.com/u/jacquie)\
**Post date:** [November 23, 2006, 8:18am UTC](https://www.ruby-forum.com/t/google-map-to-html-not-working/76995/1 "2006-11-23T08:18:20Z")

</div>

Hi,there

This question might sound a bit out-dated but I am still struggling with  
it.

When I try to display the google map in my application, I rendered  
@map.to\_html like everyone else does, but didnt get any result. The  
error shows as follows

ActionView::TemplateError (You have a nil object when you didn’t expect  
it!  
The error occured while evaluating nil.zoom=) on line #1 of  
app/views/admin/content/\_place\_map.rhtml:  
1: \<% @map.zoom = 4 %\>  
2: \<%= @map.to\_html %\>

By using the google map API,I followed the instruction in readme  
strictly. register for a new key and include the cartographer module in  
RAILS\_ROOT/config/environment.rb by require ‘cartographer’

It looks like I was able to get @map.to\_html in my controller but not  
able to render in the view.

Anyone encountered this problem before?

Thanks for any help!

---

<div class="post-metadata">

**Author:** ![jacquie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jacquie](https://www.ruby-forum.com/u/jacquie)\
**Post date:** [November 23, 2006, 11:05am UTC](https://www.ruby-forum.com/t/google-map-to-html-not-working/76995/2 "2006-11-23T11:05:23Z")

</div>

Jacquie,

It looks like your view can’t see the @map instance, any chance you are  
actually trying to make this call in a partial or that the object  
wasn’t created correctly in the contoller? Can you paste a code snippet  
for us to look at?

---

<div class="post-metadata">

**Author:** ![jacquie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jacquie](https://www.ruby-forum.com/u/jacquie)\
**Post date:** [November 23, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/google-map-to-html-not-working/76995/3 "2006-11-23T23:28:21Z")

</div>

Thanks for your reply,glenn.

Yeah,thats what I thought,but not sure why.

I have @map in my map\_controller.rb as is shown below

def index  
# create the map  
@map = Map.new(:name =\> ‘placemapper’, :width=\>800, :height =\> 600,  
:controls =\> [:zoom, :type], :draggable =\> true)  
# default type  
@map.type = :map  
# default zoom level  
@map.zoom = 13

```
places = Hash.new
place = Place.find(:first, :conditions => [%{location = ? }, 

```

params[:query].downcase])  
if !place.nil?  
longitude = place.longitude  
latitude = place.latitude  
longlat = [longitude, latitude]

```
  places[longlat] = [place.address << "," << place.suburb << "," << 

```

place.state \<\< “,” \<\< “Australia”]

```
  i = 0
  places.each { |key,value|
      text = ""
      value.each { |v| text << v }
      @map.markers << Cartographer::Marker.new( :name => 

```

“marker\_#{i}”, :position =\> key, :info\_window =\> text )  
i += 1  
}  
# center the map  
@map.center = places.keys.last #@map.center and @places.keys.last  
both are arrays  
redirect\_to :controller=\>‘admin/content’, :action  
=\>“display\_map.rjs”  
else  
redirect\_to :controller =\> ‘admin/content’, :action  
=\>“get\_more\_info.rjs”  
end  
end

Then in views/admin/content/display\_map.rjs

page.insert\_html :bottom, ‘my\_map’, :partial =\> ‘place\_map’  
page.visual\_effect :appear, ‘my\_map’

Then in views/admin/content/place\_map.rhtml

\<% @map.zoom = 4 %\>  
\<%= @map.to\_html %\>

would it be because I used rjs? but the second redirect\_to works fine.

anybody has some ideas?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![jacquie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jacquie](https://www.ruby-forum.com/u/jacquie)\
**Post date:** [November 24, 2006, 3:17am UTC](https://www.ruby-forum.com/t/google-map-to-html-not-working/76995/4 "2006-11-24T03:17:43Z")

</div>

Thanks, Scott. I solved this problem by rendering an action under the  
same controller.

Thank you all!

Scott B. wrote:

> On 11/23/06, jacquie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > ```
> > @map = Map.new(:name => 'placemapper', :width=>800, :height => 600,
> > longitude = place.longitude
> > @map.markers << Cartographer::Marker.new( :name =>
> > 
> > ```

---

<div class="post-metadata">

**Author:** ![jacquie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jacquie](https://www.ruby-forum.com/u/jacquie)\
**Post date:** [November 24, 2006, 1:58am UTC](https://www.ruby-forum.com/t/google-map-to-html-not-working/76995/5 "2006-11-24T01:58:25Z")

</div>

On 11/23/06, jacquie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @map = Map.new(:name => 'placemapper', :width=>800, :height => 600,
> longitude = place.longitude
> @map.markers << Cartographer::Marker.new( :name =>
> 
> ```
> 
> =\>“get\_more\_info.rjs”  
> \<% @map.zoom = 4 %\>  
> \<%= @map.to\_html %\>
> 
> would it be because I used rjs? but the second redirect\_to works fine.
> 
> anybody has some ideas?

It’s because you are doing a redirect. That is causing to to jump to a  
new  
page and start a brand new request, which no longer has all of the code  
you  
ran before the redirect in memory. You need to a do a render instead of  
a  
redirect.

–  
Scott B.  
Electro Interactive, Inc.  
Office: 813-333-5508

> **[Electro Interactive - Web Development. Ruby on Rails. Web Standards.](http://www.electrointeractive.com)**
>
> Electro Interactive is a full service new media design and development company.

---

<div class="post-metadata">

**Author:** ![jacquie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/35a633/32.png) [@jacquie](https://www.ruby-forum.com/u/jacquie)\
**Post date:** [November 27, 2006, 12:53am UTC](https://www.ruby-forum.com/t/google-map-to-html-not-working/76995/6 "2006-11-27T00:53:22Z")

</div>

Unfortunately I need to keep on this topic since the map is not  
displaying at all.

in my controller, i set up a breakpoint before rendering the rjs  
template.

@map.center = places.keys.last  
breakpoint  
render :action =\>‘index.rjs’

I print out the @map.to\_html at this breakpoint and it gives out

It looks fine.

However,in my webpage,I’ve only had

The webpage where it should display the map is completely blank.

anyone has the same/similar problem before?

Thanks!
