7

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user3342292
  • 129
  • 5
  • 5
    You can move the UPDATE part out into an answer to this question and accept your own question. In this way your question won't pop up as unsolved and people can upvote on your answer, if they like! :) – Fei Aug 29 '15 at 23:00
  • Please don't put an answer in the question; post your solution as an answer post below instead. I've removed your edit from the question post. – Martijn Pieters Sep 13 '16 at 13:20

1 Answers1

1

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

Ajay Barot
  • 1,681
  • 1
  • 21
  • 37
  • 2
    if you are going to do that, better make the post a *community wiki* post, and add a note that this was taken from the OP's post. See [Best way to deal with questions answered by the OP in the question instead of in an answer](https://meta.stackexchange.com/q/108969). I've converted this post now. – Martijn Pieters Sep 13 '16 at 13:21
  • @MartijnPieters: Thank you very much. – Ajay Barot Sep 13 '16 at 13:21