【问题标题】:Rails 4 gmaps4rails - How to include a link to a "show" view in marker infowindow in gmaps4rails gemRails 4 gmaps4rails - 如何在 gmaps4rails gem 的标记信息窗口中包含指向“显示”视图的链接
【发布时间】:2016-09-13 21:37:27
【问题描述】:

我已经让 gmaps4rails 在我的 rails 4 应用程序中工作,带有用于我的位置的标记,我想在每个位置的标记中包含一个指向每个位置的单独显示视图的链接。

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

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) %>

我的空间/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>

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

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

根据我之前的搜索,这似乎是让链接在标记的信息窗口中工作的方法,但如果有替代解决方案,我很想听听,谢谢。

【问题讨论】:

  • 您可以将 UPDATE 部分移到此问题的答案中并接受您自己的问题。这样一来,您的问题就不会因为未解决而弹出,如果他们愿意,人们可以对您的答案进行投票! :)
  • 请不要在问题中回答;将您的解决方案作为答案发布在下面。我已从问题帖子中删除了您的编辑。

标签: ruby-on-rails google-maps ruby-on-rails-4 gmaps4rails


【解决方案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

局部视图应如下所示:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
相关资源
最近更新 更多