0

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

1 に答える 1

0

反復するとき、現在、各 オブジェクトに対して同じレストラン( @restaurant) を取得していますMapmap.restaurant代わりに使用して、正しいレストランを取得してください。

また、手動で作成するのではなく、Rails の url-helpers を使用して URL を作成することをお勧めします。

最後に、コントローラー内のオブジェクトに「生の」html を書き込むのは良くありません。シバン全体を(少なくとも)ヘルパーメソッドに移動することをお勧めします。

だから、このようなもの:

Map.all.to_gmaps4rails do |map, marker|
  marker.infowindow info_for_restaurant(map.restaurant)
end

# Somewhere else in your controller
private 
def info_for_restaurant(restaurant)
  view_context.link_to restaurant_path(restaurant) do
    view_context.content_tag("h2") do
      restaurant.name
    end
  end
end
于 2013-03-29T13:11:10.610 に答える