2013-12-16 69 views
0

控制器:Gmaps4Rails JSON請求模板丟失

# 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 

舊版本的寶石(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 @位置。試圖升級到最新版本血紅素站在1.5.6,2.1.1現在。當你考慮如何行事?

+0

模板犯規存在嗎? – apneadiving

+0

你好,有人在嗎? – apneadiving

+0

存在模板。如果請求類型填充了html,這很好。但我也使用json請求來執行此控制器操作。在這種情況下,只會出現問題。 – galievruslan

回答

0

嘗試:

marker.infowindow render_to_string(:partial => "/customers/location", :formats => [:html], :locals => { :object => location}) 
+0

這很簡單。謝謝 – galievruslan