# Gmaps on Rails

**URL:** <https://www.ruby-forum.com/t/gmaps-on-rails/89940>\
**Category:** Rails\
**Created:** [March 24, 2007, 12:19am UTC](https://www.ruby-forum.com/t/gmaps-on-rails/89940 "2007-03-24T00:19:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [March 24, 2007, 12:19am UTC](https://www.ruby-forum.com/t/gmaps-on-rails/89940/1 "2007-03-24T00:19:55Z")

</div>

I’m working with the Google Maps API and picked up the book “Beginning  
Google Maps Applications with Rails & AJAX”. I have encountered an  
interesting problem.

I’m working in chapter 3 (for anyone who has the book) and it shows how  
to build a rails app that allows for users to create string-tagged  
markers and save them to a database.

I built the app and it works fine when I run it locally in Firefox.  
However, if I run it locally in IE, something goes wrong and I get my  
error message from the try/catch block.

I uploaded the app to my website to test it there and it does not work  
in either Firefox or IE. You can see it here:  
[http://www.map.nvsagebrush.com](http://www.map.nvsagebrush.com)

Has anyone encountered this before and now what I’m doing wrong?

The application.js contains the javascript necessary to manipulate the  
google map while the controller saves the marker’s coordinates and  
related string to the database.

Here is the code in question:

##### application.js

function storeMarker(){  
var lng = document.getElementById(“longitude”).value;  
var lat = document.getElementById(“latitude”).value;

```
var getVars = "?m[tag]=" + document.getElementById("tag").value
    + "&m[lng]=" + lng
    + "&m[lat]=" + lat ;

var request = GXmlHttp.create();

//call the store_marker action back on the server
request.open('GET', 'create' + getVars, true);
request.onreadystatechange = function() {
    if (request.readyState == 4) {
        //the request is complete

        var success=false;
        var content='Error contacting web service';
        try {
          //parse the result to JSON (simply by eval-ing it)
          res=eval( "(" + request.responseText + ")" );
          content=res.content;
          success=res.success;
        }catch (e){
          success=false;
        }

        //check to see if it was an error or success
        if(!success) {
            alert(content);
        } else {
            //create a new marker and add its info window
            var latlng = new

```

GLatLng(parseFloat(lat),parseFloat(lng));  
var marker = createMarker(latlng, content);  
map.addOverlay(marker);  
map.closeInfoWindow();  
}  
}  
}  
request.send(null);  
return false;  
}

##################################

###### map\_controller.rb

class ChapThreeController \< ApplicationController  
def create  
marker = Marker.new(params[:m])  
if marker.save  
res={:success=\>true,:content=\>“

**Tag:**  
 #{marker.tag}
”}  
else  
res={:success=\>false,:content=\>“Could not save the marker”}  
end  
render :text=\>res.to\_json  
end

def list  
render :text=\>(Marker.find :all).to\_json  
end  
end  
##################################

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [March 24, 2007, 1:37am UTC](https://www.ruby-forum.com/t/gmaps-on-rails/89940/2 "2007-03-24T01:37:33Z")

</div>

I went ahead and called

alert(request.responseText);

I found this to be a problem:

Routing Error  
No route found to match “/create” with {:method=%gt;:get}

Unfortunately I don’t see anything wrong. Any thoughts?

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [March 24, 2007, 1:46am UTC](https://www.ruby-forum.com/t/gmaps-on-rails/89940/3 "2007-03-24T01:46:07Z")

</div>

Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I went ahead and called
> 
> alert(request.responseText);

Good debugging!

> I found this to be a problem:
> 
> Routing Error  
> No route found to match “/create” with {:method=%gt;:get}
> 
> Unfortunately I don’t see anything wrong. Any thoughts?

I had a problem with this too… Here’s what I did:

map.upload ‘upload/:sid/:file’,  
:controller =\> “upload”,  
:action =\> “upload”,  
:store =\> nil,  
:requirements =\> { :file =\> /[^/]+/ }

Note the “requirements” section… it’s a regular expression to tell  
rails  
what format to expect for that parameter. I’m guessing either the  
greater-than sign or the colon is confusing rails.

On that note, I’m curious about the %gt; too… I would have expected  
\> or  
a percent-escaped hex code…

Cheers,  
Tyler

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [March 24, 2007, 1:57am UTC](https://www.ruby-forum.com/t/gmaps-on-rails/89940/4 "2007-03-24T01:57:42Z")

</div>

Tyler MacDonald wrote:

> I had a problem with this too… Here’s what I did:
> 
> map.upload ‘upload/:sid/:file’,  
> :controller =\> “upload”,  
> :action =\> “upload”,  
> :store =\> nil,  
> :requirements =\> { :file =\> /[^/]+/ }
> 
> Note the “requirements” section… it’s a regular expression to tell  
> rails  
> what format to expect for that parameter. I’m guessing either the  
> greater-than sign or the colon is confusing rails.

Ok then. I’m not too good at writing routes. Since my controller is  
named map\_controller.rb I would have something like:

```
  map.map 'map/:sid/:file',
     :controller => "map",
     :action => "create",
     :store => nil,
     :requirements => { :file => /[^\/]+/ }

```

But what is going on here: ‘map/:sid/:file’

> On that note, I’m curious about the %gt; too… I would have expected  
> \> or  
> a percent-escaped hex code…

Typo on my part, it was \>

Thanks for the help.

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [March 24, 2007, 3:48am UTC](https://www.ruby-forum.com/t/gmaps-on-rails/89940/5 "2007-03-24T03:48:39Z")

</div>

The parameters that are going through the Webrick terminal are as  
follows:

/create?m[found]=1&m[left]=2&m[lng]=-120.146484375&m[lat]=39.081040177486074

It is obviously parsing the ‘/create’ part since that is in the error  
but I’m not sure what is going on from there.

---

<div class="post-metadata">

**Author:** ![colinloretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a698b9/32.png) [@colinloretz](https://www.ruby-forum.com/u/colinloretz)\
**Post date:** [March 24, 2007, 5:02am UTC](https://www.ruby-forum.com/t/gmaps-on-rails/89940/6 "2007-03-24T05:02:24Z")

</div>

Well I certainly feel foolish now, especially after spamming my own  
post.

I came back to my app after a quick break and typed the local url into  
the browser and voila! there was the map with all the markers supplied  
from the database.

The issue was with the routes, but not as complex as all of this:

> map.upload ‘upload/:sid/:file’,  
> :controller =\> “upload”,  
> :action =\> “upload”,  
> :store =\> nil,  
> :requirements =\> { :file =\> /[^/]+/ }

but thank you for trying to help.

The issue?  
I had routed the ChapThree controller to be the new index which should  
have made the following work:  
[http://map.nvsagebrush.com/](http://map.nvsagebrush.com/)

Unfortunately, the routing wasn’t working properly

I then tried:  
[http://map.nvsagebrush.com/chap\_three](http://map.nvsagebrush.com/chap_three) ← Same problem

Finally:  
[http://map.nvsagebrush.com/chap\_three/](http://map.nvsagebrush.com/chap_three/) ← Voila and how stupid do you  
think I feel?
