Controller:
# GET /customers/1
# GET /customers/1.json
def show
@customer = Customer.find(params[:id])
@locations = Gmaps4rails.build_markers(@customer.shipping_addresses) do |location, marker|
marker.lat location.latitude
marker.lng location.longitude
marker.infowindow render_to_string(:partial => "/customers/location", :locals => { :object => location})
marker.title location.name.to_s
end
# @locations = @customer.shipping_addresses.to_gmaps4rails do |location, marker|
# marker.infowindow render_to_string(:partial => "/customers/location", :locals => { :object => location})
# marker.title location.name.to_s
# end
respond_to do |format|
format.html # show.html.erb
format.json { render json: @customer }
end
end
Old version gem(1.5.6) code commented
View:
<div style='width: 800px;'>
<div id="map" style='width: 800px; height: 400px;'></div>
</div>
<script type="text/javascript">
jQuery(document).ready(function() {
handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
markers = handler.addMarkers(<%=raw @locations.to_json %>);
handler.bounds.extendWith(markers);
handler.fitMapToBounds();
});
});
</script>
Everything works fine. But if you make a request to the json page. What if there will shipping_address coordinates in error:
Template is missing
Missing partial /customers/location with {: locale => [: ru],: formats => [: json],: handlers => [: erb,: builder,: coffee]}. Searched in: * "/ team / mss / app / views" * "/ usr/local/rvm/gems/ruby-1.9.3-p448/gems/twitter-bootstrap-rails-2.2.6/app/views" * "/ usr/local/rvm/gems/ruby-1.9.3-p448/gems/devise-2.2.4/app/views" * "/ usr/local/rvm/gems/ruby-1.9.3-p448/gems / kaminari-0.14.1/app/views "
Alternatively I can do a background check on the controller format.
unless request.format.json?
@locations = Gmaps4rails.build_markers(@customer.shipping_addresses) do |location, marker|
marker.lat location.latitude
marker.lng location.longitude
marker.infowindow render_to_string(:partial => "/customers/location", :locals => { :object => location})
marker.title location.name.to_s
end
end
And if he is not doing json @ locations. Tried to upgrade to the latest version heme stood before 1.5.6, 2.1.1 now. As you consider how to act?
Try:
marker.infowindow render_to_string(:partial => "/customers/location", :formats => [:html], :locals => { :object => location})