帮助在查看显示餐厅 ID 的地图上创建与名称餐厅的链接。控制器/maps_controller.rb
def index
@maps = Map.all
@json = Map.all.to_gmaps4rails do |map, marker|
marker.infowindow "<a href=/restaurants/#{@restaurant.object_id}><h2>#{map.name}</h2></a>"
end
并创建与特定 id 餐厅视图 show
views\restaurants\show.html.erb的关系
<%= @restaurant.title %>
路线.rb
resources :restaurants
resources :maps
数据库表
create_table "maps", :force => true do |t|
t.string "name"
t.string "address"
t.float "longitude"
t.float "latitude"
t.boolean "gmaps"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "restaurants", :force => true do |t|
t.string "title"
t.text "description"
t.string "image_url"
t.integer "map_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
和模型
class Map < ActiveRecord::Base
attr_accessible :address, :gmaps, :latitude, :longitude, :name
acts_as_gmappable
has_one :restaurant
def gmaps4rails_address
address
end
end
class Restaurant < ActiveRecord::Base
attr_accessible :description, :image_url, :title, :map_id
belongs_to :map
end