Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 541
user3342292 Avatar asked Jun 22 '15 20:06

user3342292


1 Answers

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

like image 176
3 revs, 2 users 89% Avatar answered Oct 20 '22 02:10

3 revs, 2 users 89%