2

I'm trying to figure out how to use gmaps for rails in my Rails 4 app. I have posted several questions on SO in relation to the setup problems I've been facing over the course of the last 3 months. So far, I haven't found the help I need to try to get this working in my app. Given how simple people seem to suggest this setup is, I'm posting my full use case to see if someone might be able to spot a problem with my structure or use case (as opposed to problems plugging in the gem).

I have models for Address, Project, Profile and Participant. The associations are:

Address

geocoded_by :full_address   # can also be an IP address
belongs_to :addressable, :polymorphic => true

Project

has_many :addresses, as: :addressable
      accepts_nested_attributes_for :addresses,  reject_if: :all_blank, allow_destroy: true
has_one :package

Profile

has_many :addresses, as: :addressable
    accepts_nested_attributes_for :addresses,  reject_if: :all_blank, allow_destroy: true

Participant

has_many :addresses, as: :addressable
    accepts_nested_attributes_for :addresses,  reject_if: :all_blank, allow_destroy: true
belongs_to :package

In my Gemfile, I have:

gem 'geocoder'
gem 'gmaps4rails'#, '~> 2.1', '>= 2.1.2'

In my application.js, I have

//= require underscore
//= require gmaps/google
//= require markerclusterer
//= require infobox

In my app/vendor/assets/javascripts folder, I have the underscore, infobox and marker clusterer files. I have done that based on the advice in this one of my posts.

In my address view folder I have a partial called new_test_fix_map.html.erb:

<script src="//maps.google.com/maps/api/js?v=3.23&key=<%= ENV['GOOGLE_MAPS_API_KEY'] %>"></script>

 <!-- <script src="//maps.google.com/maps/api/js?v=3.18&sensor=false&client=&key=&libraries=geometry&language=&hl=&region="></script>
 -->
<div style='width: 800px;'>
  <div id="map" style='width: 800px; height: 400px;'></div>
</div>

<script>
    markers = handler.addMarkers();
</script>

<script type="text/javascript">
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>

In my profile show page, I have:

<%= render partial: "addresses/new_test_map_fix" %>

In my profile controller show action, I have:

def show
    # debugger
    @addresses = @profile.addresses

      @hash = Gmaps4rails.build_markers(@addresses) do |address, marker|
        marker.lat address.latitude
        marker.lng address.longitude
        marker.infowindow address.full_address
      end
  end

In my project show page, I have:

<%= render partial: "addresses/new_test_map_fix" %>

In my project controller show action, I have:

def show
    @project = Project.find(params[:id])
    authorize @project
    @addresses = @project.addresses

      @hash = Gmaps4rails.build_markers(@addresses) do |address, marker|
        marker.lat address.latitude
        marker.lng address.longitude
        marker.infowindow address.full_address
  end

In my address controller I have:

def show
    @hash = Gmaps4rails.build_markers(@addresses) do |address, marker|
      marker.lat address.latitude
      marker.lng address.longitude
    end
  end

In my address model I have:

def full_address
    [self.first_line, middle_line, last_line, country_name].compact.join("<br>").html_safe
  end

In my participants view, I have:

<% if @project.package.participant.location_specific == true %>
    <%= @project.package.participant.addresses.first.full_address %>
    <%= render partial: 'addresses/new_test_fix_map' %>
<% else %>
   Remote participation available
<% end %>

In my participants controller I have:

def show
    @addresses = @participant.addresses

      @hash = Gmaps4rails.build_markers(@addresses) do |address, marker|
        marker.lat address.latitude
        marker.lng address.longitude
        marker.infowindow address.full_address
      end

Currently, when I try to render a map on the profile show page, everything works. I actually don't understand why given that profile has many addresses and I haven't asked for the first address yet. Anyway, when I try render a map on the projects show page, i get no errors but no map. The setup for projects and profiles, so far as address is concerned is exactly the same. It should work for both if it works for one. It doesnt work at all for project. Also, I can't get it to work for participants, but I suspect that if I can solve this problem for project then I will find the solution for participants.

When I search in the console, I can do:

p = Profile.first
p.addresses

I can also do:

p = Project.first
p.addresses

In both cases, the address is returned. On the projects and profiles show page, the printed text of the address displays properly. On the profile show page, the map renders properly.

On the projects show page, I can see from the chrome inspector that the address is being populated properly in the handler (together with a latitude and longitude value in that handler), but the map does not render.

Can anyone offer any advice for how to explore this problem?

Community
  • 1
  • 1
Mel
  • 2,481
  • 26
  • 113
  • 273
  • I think you can take reference from [here](https://spunkylive@bitbucket.org/spunkylive/googlemapdemo.git) – Anuj Sep 02 '16 at 09:01

1 Answers1

0

Well just off the top I notice that @particpant is not defined in the participants controller show action. So when @adresses looks for @participant it may just simply be returning nothing which could be the problem.

Otherwise I had an issue with something like this on a previous project and one page loaded but the other didn't and it turned out that it was an issue of Turbolinks causing the first page to load the script but the 2nd page to not. For further information on that, see: GMaps4Rails and Turbolinks not loading without full page refresh

Community
  • 1
  • 1
Stephen W
  • 244
  • 2
  • 7
  • Hi Stephen. Thanks for the suggestion. I have turbo links set to false (it clashes with olark). The second page never works for me, even if I restart everything and go to that page first. – Mel Sep 04 '16 at 22:37