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=®ion="></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?