Rails 4 gmaps4rails - How to include a link to a "show" view in marker infowindow in gmaps4rails gem

689 views Asked by At

I have gotten gmaps4rails working in my rails 4 app, with markers for my lcoation, and I would like to include a link in each location's marker to each location's individual show view.

My spaces controller index action looks like this:

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

My infowindow partial is:

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

My gmaps4rails javascript script in my spaces/index.html view is:

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

When I try to load my spaces index page I am greeted with the following error message:

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

Based on my previous searches this seems to be the way to get a link working in a marker's infowindow but if there is an alternative solution I would love to hear it, thank you.

1

There are 1 answers

2
Ajay Barot On

Originally posted by the OP in the question itself

spaces controller index action should looks like this :

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

and partial view should looks like this:

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