ビューのレストラン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