Rails 4 gmaps4rails - 如何在gmaps4rails gem中的标记infowindow中包含指向"show"视图的链接

use*_*292 8 google-maps ruby-on-rails gmaps4rails ruby-on-rails-4

我已经在我的rails 4应用程序中使用了gmaps4rails,我的lcoation有标记,我想在每个位置的标记中包含一个链接到每个位置的各个节目视图.

我的空间控制器索引操作如下所示:

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
Run Code Online (Sandbox Code Playgroud)

我的信息部分是:

<%= link_to "Show", space_path(space) %>
Run Code Online (Sandbox Code Playgroud)

我的space/index.html视图中的gmaps4rails javascript脚本是:

<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>
Run Code Online (Sandbox Code Playgroud)

当我尝试加载我的空间索引页面时,我收到以下错误消息:

undefined local variable or method `space' for #<#<Class:0x4638c48>:0x5335f20>
Run Code Online (Sandbox Code Playgroud)

基于我之前的搜索,这似乎是在标记的信息窗口中获取链接的方法,但如果有替代解决方案,我很乐意听到它,谢谢.

Aja*_*rot 1

最初由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
Run Code Online (Sandbox Code Playgroud)

部分视图应该如下所示:

<%= link_to 'See Space', space_path(object) %>
Run Code Online (Sandbox Code Playgroud)

  • 如果您打算这样做,最好将该帖子设为*社区 wiki* 帖子,并添加一条注释,表明该帖子取自 OP 的帖子。请参阅[处理OP在问题中而不是在答案中回答的问题的最佳方法](https://meta.stackexchange.com/q/108969)。我现在已经转换了这篇文章。 (2认同)