2015-06-22 46 views
8

我已經在我的rails 4應用程序中使用了gmaps4rails,併爲我的lcoation添加了標記,並且我希望在每個位置的標記都顯示在每個位置的個別顯示視圖中。Rails 4 gmaps4rails - 如何在gmaps4rails中的標記infowindow中包含指向「show」視圖的鏈接gem

我的空間控制指標的行動看起來是這樣的:

def index 
    @spaces = Space.all 
    @hash = Gmaps4rails.build_markers(@spaces) do |space, marker| 
     marker.lat space.latitude 
     marker.lng space.longitude 
     marker.infowindow render_to_string(:partial => "/spaces/infowindow", :locals => { :object=> space}) 
    end 
end 

我的信息窗口的部分是:

<%= link_to "Show", space_path(space) %> 

我gmaps4rails在我空間的javascript腳本/ index.html的觀點是:

<script> 
handler = Gmaps.build('Google'); 
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){ 
    markers = handler.addMarkers(<%=raw @hash.to_json %>); 
    handler.bounds.extendWith(markers); 
    handler.fitMapToBounds(); 
}); 
</script> 

當我嘗試加載空間索引頁面時,我收到以下錯誤消息:

undefined local variable or method `space' for #<#<Class:0x4638c48>:0x5335f20> 

根據我以前的搜索,這似乎是得到一個鏈接標記的信息窗口工作的方式,但如果有一個替代的解決方案我很想聽到它,謝謝。

+5

您可以將UPDATE部分移出到答案中回答這個問題並接受你自己的問題。通過這種方式,您的問題不會像未解決的問題那樣彈出,如果他們喜歡,人們可以對您的答案滿意! :) – zhurora

+0

請不要在問題中回答問題;相反,請將您的解決方案作爲答案帖子發佈。我已經從問題文章中刪除了您的修改。 –

回答

2

最初由OP張貼在問題本身

空間控制指標的行動應該是這樣的:

def index 
    @spaces = Space.all 
    @hash = Gmaps4rails.build_markers(@spaces) do |space, marker| 
     marker.lat space.latitude 
     marker.lng space.longitude 
     marker.json({:id => space.id }) 
     marker.infowindow render_to_string(:partial => "/spaces/infowindow", :locals => { :object => space}) 
    end 
end 

和部分應視圖看起來是這樣的:

<%= link_to 'See Space', space_path(object) %> 

+2

如果你打算這樣做,最好使帖子*社區維基*帖子,並添加一個注意到,這是從OP的職位。請參閱[最好的方式來處理OP在問題中而不是在答案中回答的問題](https://meta.stackexchange.com/q/108969)。我已經轉換了這篇文章。 –

+0

@MartijnPieters:非常感謝。 –

相關問題