Question

I am working on RoR and using Ruby Geocoder for IP identification. The code snippet I use giving below.

Gemfile

gem "geocoder"

Then for fetching country I am using

country = request.location.country

It was working perfectly before,but now it is throwing an error like undefined method country for nil class.

I am not able to figure out the issue. Can any one help me to find out the solution or suggest any other gem on API for finding country based on IP Address

Thanks
Regards

Was it helpful?

Solution

There seems to have been an issue with Geocoder service. Even my application that runs on AWS was giving same problem for last 36'ish hours. Its all working well now. I didn't do anything, that makes me imply there was something wrong with Geocoder which they have apparently fixed now.

I wonder if this has happened in the past as it certainly worries me.

OTHER TIPS

If you are on a local development environment, request.location will more than likely return nil. According to the docs:

Note that [request.location] will usually return nil in your test and development environments because things like "localhost" and "0.0.0.0" are not an Internet IP addresses.

Accordingly, the particular issues involved with nil exception should – for the most part – be limited to development environments. While developing locally, nil exceptions can handled using Ruby exception handling or the Rails try method:

request.location.try(:country)

UPDATE:

As @nonocut explains in his answer, this issue cited in the question may be due to some sort of problem with Google Maps. Nevertheless, it's important to be able to handle these outages via exception handling – it's good for local development, and it makes for pragmatic deployment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top