控制器:
# 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
旧版gem(1.5.6)代码评论了
查看:
<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>
一切正常。但是如果你向json页面发出请求。如果出错的shipping_address坐标怎么办:
缺少模板
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 "
或者我可以对控制器格式进行背景检查。
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
如果他不是在做json @ locations。试图升级到最新版本的heme现在在1.5.6,2.1.1之前。在考虑如何行动?
答案 0 :(得分:0)
尝试:
marker.infowindow render_to_string(:partial => "/customers/location", :formats => [:html], :locals => { :object => location})