Reputation: 963
Hello I have included following gem in my gemfile
gem 'geocoder'
and written following code in my view file
- @location = Geocoder.search(request.remote_ip).first.country
%div{"class" => "currentCountry", "value" => "#{@location}"}
- if session[:browser] == 'mobile'
testmobile
- else
testdesktop
and in heroku server it gives me error while in my local server its working fine.
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}"}
Please guide why I am facing this error in heroku server.
Upvotes: 1
Views: 298
Reputation: 601
I went through this before, its because the request is getting timed out and the response is nil. The reasons are :-
Freegeoip as its a free service is usually overloaded and sometimes might respond late and as the default timeout is set as 3 seconds the request gets timedout and it returns nil as a response which causes this issue(same has been discussed here).
Solutions :-
Both the options ip_lookup and timeout can be configured in the initializer (check here for more information, thanks.
Upvotes: 1
Reputation: 882
geocoder_result = Geocoder.search(request.remote_ip)
@location = geocoder_result.empty? ? "" : geocoder_result.first.country
Upvotes: 1