Topic: Googlemap with database

I'm going to bring this down to the basics because I've tried everything but nothing is working.

I created a admin controller for scaffolding (so I can add things to a database).  For google maps, this includes the latitude and longitude.  I created a restaurant controller to take that information and display it.  For some reason, the restaurant controller is not collecting the data from the database.  I receive a nil value when I try to run the application from the line <%@restaurants.each do |restaurant|%>.  Any suggestions would be of great help.

--migration--
class CreateRestaurants < ActiveRecord::Migration
  def self.up
    create_table :restaurants do |t|
      t.column :category, :string
      t.column :name, :string
      t.column :address, :string
      t.column :phone, :string
      t.column :hours, :text
      t.column :more_info, :text
      t.column :latitude, :float
      t.column :longitude, :float
    end
  end

  def self.down
    drop_table :restaurants
  end
end

--Restaurant_controller--
class RestaurantController < ApplicationController

  def index
    @restaurant = Restaurant.find(:all).to_json
  end
end

--restaurant layout--
~~~normal html~~~
<script type="text/javascript">var markers=<%= @restaurant.to_json %>;</script>
~~~normal html~~~

--index.rhtml--
<ul id="sidebar-list">
    <%@restaurants.each do |restaurant|%>
        <li id="sidebar-item-<%restaurant.id%>">
            <%=link_to_function "<strong>#{restaurant.name}</strong> #{restaurant.address}", "focusPoint(#{restaurant.id})"%>
        </li>
    <%end%>
</ul>

--map.js--
var map;
var centerLatitude = 42.733868932421856;
var centerLongitude = -84.47994947433472;
var startZoom = 16;
var markerHash = {};
var currentFocus = false;

function focusPoint(id) {
    if (currentFocus) {
        Element.removeClassName("sidebar-item-"+currentFocus,"current");
    }
    Element.addClassName("sidebar-item-"+id,"current");
    markerHash[id].marker.openInfoWindowHtml(markerHash[id].address);
    currentFocus=id;
}

function addMarker(latitude, longitude, id) {
    var marker = new GMarker(new GLatLng(latitude, longitude));
   
    GEvent.addListener(marker, 'click',
    function() {
        focusPoint(id);
    });
    map.addOverlay(marker);
    return marker;
}

function init() {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom, G_HYBRID_MAP);
       
        for (var i=0; i<markers.length; i++) {
            var current = markers[i];
            marker = addMarker(current.latitude, current.longitude, current.id);
            markerHash[current.id]={marker:marker, address:current.address, visible:true};
        }
    }
}

window.onload = init;
window.onunload = GUnload;

Re: Googlemap with database

this:

  def index
    @restaurant = Restaurant.find(:all).to_json
  end

should be this:
  def index
    @restaurants = Restaurant.find(:all)
  end

Apply the .to_json conversion only when you're sticking this array into the javascript.

Let me know if this fixes things!

Re: Googlemap with database

wow.  I thought I tried every possible combination but this seemed to do it.  It outputted my list of locations in the sidebar but I have to fix the javascript so it puts the points on the map.

Thank you