Greg
Greg

Reputation: 2609

Route in Rails for json depends on where called from

A file maps.json is created and is used in the following JavaScript

function findSelectedMap(mapID, cb) {
   $.getJSON('maps.json', function(json) { 
    json.forEach(function(entry) {
      if (entry.maps.id == mapID) {
        changeLayerTo = entry.maps.url;
        maxZoom = entry.maps.zoom;
      }
    });
    cb();
  });
};

It works fine when called from the top level http://localhost:3000/overview with mapId being

mapID = $("#select-overlay input[type='radio']:checked").val();

But when called from http://localhost:3000/streets/45, the error is http://localhost:3000/streets/streets/maps.json 404 (Not Found). I tried get 'streets/maps.json' => 'maps.json' and similar in routes.Redondo Beach without success. I suppose I could force maps.json to be in streets and do a if exists (whatever the jS is), but that seems to cumbersome. Is there a way around this?

Upvotes: 1

Views: 59

Answers (1)

yeuem1vannam
yeuem1vannam

Reputation: 957

You can add the slash to the beginning of the endpoint to let browser know your URL starting from base URL

$.getJSON('/maps.json', function(json) {
  // rest of logic
})

Upvotes: 2

Related Questions