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.