Gmaps on Rails

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

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
##################################

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?

Colin L. [email protected] 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

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.

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.

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/

Unfortunately, the routing wasn’t working properly

I then tried:
http://map.nvsagebrush.com/chap_three ← Same problem

Finally:
http://map.nvsagebrush.com/chap_three/ ← Voila and how stupid do you
think I feel?