嗨,我目前正在使用 gmap4rails 完成本教程
我对这个宝石很陌生,所以我只是一步一步地阅读指南。
我有一个 city_controller.rb
class CitiesController < ApplicationController
def index
@cities = City.all
@json = @cities.to_gmaps4rails
end
end
我的模型有一个 city.rb
class City < ActiveRecord::Base
acts_as_gmappable
has_many :neighborhoods
def gmaps4rails_address
"#{self.name}, #{self.state}"
end
end
这是我的视图文件夹中的城市文件夹中的 index.html.erb
<%= gmaps4rails(@json) %>
我添加了 gem 也运行了安装程序。
在我的应用程序布局中,我的身体中有这个
<%= yield :scripts %>
这是我的架构
ActiveRecord::Schema.define(version: 20130714194244) do
create_table "cities", force: true do |t|
t.string "name"
t.string "state"
t.float "latitude"
t.float "longitude"
t.boolean "gmaps"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "population"
end
我实际上没有收到任何错误,我用这些为数据库播种
City.create(name: "New york" ,state: "NY", population: 8175133)
City.create(name: "Los Angeles",state: "CA", population: 3792621 )
City.create(name: "Chicago",state: "IL", population: 2695598)
City.create(name: "Houston",state: "TX", population: 2099451)
City.create(name: "Philadelphia",state: "PA" , population: 1526006)
City.create(name: "Phoenix",state: "AZ", population: 1445632)
City.create(name: "San Antonio",state: "TX", population: 1327407)
City.create(name: "San Diego",state: "CA", population: 1307402 )
City.create(name: "Dallas",state: "TX", population: 1197816)
City.create(name: "San Jose",state: "CA", population: 945942)
当我打开 Rails 控制台并执行 City.all
我注意到已经为我计算了纬度和经度,这意味着宝石工作正常。
#<City id: 1, name: "New york", state: "NY", latitude: 40.7143528, longitude: -74.00597309999999, gmaps: true, created_at: "2013-07-14 19:51:32", updated_at: "2013-07-14 19:51:32", population: 8175133>
我错过了一些 api 密钥吗?当我做 rails s 时,我得到的只是一个空白屏幕?至少我应该看到一张地图。这可能是一个js问题,我不太确定。
谢谢!