2016-02-16 48 views
1

你好,我已經包括以下在我的Gemfile寶石地理編碼寶石創建問題

寶石「地理編碼器」

在我看來,寫入文件的下面的代碼

- @location = Geocoder.search(request.remote_ip).first.country 
%div{"class" => "currentCountry", "value" => "#{@location}"} 
- if session[:browser] == 'mobile' 
    testmobile 
- else 
    testdesktop 

和在Heroku服務器,它給了我錯誤,而在我的本地服務器工作正常。

ActionView::Template::Error (undefined method `country' for nil:NilClass): 
2016-02-16T07:47:14.447459+00:00 app[web.1]: 
2016-02-16T07:47:14.447455+00:00 app[web.1]:  3: - if session[:browser] == 'mobile' 
2016-02-16T07:47:14.447456+00:00 app[web.1]:  4: = render 'mobile' 
2016-02-16T07:47:14.447457+00:00 app[web.1]: app/views/spree/checkout/edit.html.haml:1:in `_b6865a451d7a5a040f6c1f6376727300' 
2016-02-16T07:47:14.447458+00:00 app[web.1]: 
2016-02-16T07:47:14.447454+00:00 app[web.1]:  1: - @location = Geocoder.search(request.remote_ip).first.country 
2016-02-16T07:47:14.447454+00:00 app[web.1]:  2: %div{"class" => "currentCountry", "value" => "#{@location}"} 

請指導爲什麼我在heroku服務器中遇到此錯誤。

回答

1

我之前經歷過這個,它是因爲請求超時而響應爲零。其理由是: -

  • :ip_lookup(默認 - freegeoip)
  • :超時(默認 - 3秒)

Freegeoip作爲其一個免費的服務通常是過載,並且有時可能會延遲響應,並且由於默認超時設置爲3秒,請求獲得超時,並且返回nil作爲導致此問題的響應(同樣討論了here)。

解決方案: - 。

  • 切換到一些付費IP查找服務(其他選項here),以獲得更好,更快的服務(推薦)
  • 繼續freegeoip但增加超時7-8秒因此該請求將不會很快得到已逾時(不建議在生產應用)

兩個ip_lookup和超時可以在初始化(檢查here更多信息配置的選項,謝謝。

1
geocoder_result = Geocoder.search(request.remote_ip) 

@location = geocoder_result.empty? ? "" : geocoder_result.first.country