heroku服务器

时间:2016-02-16 07:57:06

标签: ruby-on-rails heroku rails-geocoder

您好我在我的gemfile中包含了以下gem

  

gem'geocoder'

并在我的视图文件中编写以下代码

- @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服务器中遇到此错误的原因。

2 个答案:

答案 0 :(得分:1)

geocoder_result = Geocoder.search(request.remote_ip)

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

答案 1 :(得分:1)

之前我经历过这个,因为请求超时并且响应为零。原因是: -

  • :ip_lookup (默认为freegeoip)
  • :超时(默认为3秒)

Freegeoip作为一个免费服务通常是重载的,有时可能会迟到,因为默认超时设置为3秒,请求得到timedout,并返回nil作为响应导致此问题(同样已经讨论过{{3 }})。

解决方案: -

  • 切换到某些付费IP查询服务(其他选项here)以获得更好更快的服务。(推荐)
  • 继续使用freegeoip,但将超时时间增加到7-8秒,以便请求不会很快超时(不建议用于生产应用程序)

可以在初始化程序中配置选项ip_lookup和timeout(检查here以获取更多信息,谢谢。