Search code examples
ruby-on-railsgoogle-mapsgoogle-maps-markerscancangmaps4rails

Gmaps4rails marker depending on current user


Is there a way to view markers using gmaps4rails gem, depending on the current user login? I'm not sure if there was any chance it might have worked out, but I tried using the cancan gem in combination but it only results in all of the markers disappearing. Unfortunately I have no other idea how to carry on. So what I am trying to achieve is to make markers visible depending on which user is currently logged in. I am grateful for any help!

Current code of relations_controller:

    def index

    @relations = Relation.all

@hash1 = Gmaps4rails.build_markers(@relations) do |relation, marker|
    # something like "if current_user == relation.user_id" ?
    marker.lat relation.childlat
    marker.lng relation.childlng

    marker.picture({
              :url    => ActionController::Base.helpers.image_path("childgross.png"),
              :width  => "45",
              :height => "45"
             })
 end

@hash2 = Gmaps4rails.build_markers(@relations) do |relation, marker|
    marker.lat relation.kigalat
    marker.lng relation.kigalng
    marker.picture({
            :url    => ActionController::Base.helpers.image_path("persongross.png"),
            :width  => "45",
            :height => "45"
            })
 end
end

code in relations index view:

    <%=raw @hash1.to_json %>
    <%=raw @hash2.to_json %>

<script>
handler = Gmaps.build('Google');
handler.buildMap({provider: {},internal: {id: 'map'}},function(){
markers = handler.addMarkers(<%=raw @hash1.to_json %>);
markers = handler.addMarkers(<%=raw @hash2.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
</script>

Solution

  • Thnaks to @Pablo It just came to me... I just had to replace @relations = Relation.all with @relations = Relation.where(user_id: current_user.id)