Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-4rails-geocoder

Geocoder Gem - getting "Reserved" for request.location.country (even in Local!)


I need to be able to see locally the output of geocoder countries and cities for a message like 'Hi, Yu are in {country detected}.

To be able to use geocoder in local/dev mode,I have followed some SO advice and put:

in config/environment/development.rb

class ActionDispatch::Request
  def remote_ip
    "84.34.156.155" # fake ip for example                                                                                                                                                                                                                                                                                    
  end
end

and in application_controller

  def lookup_ip_location
    if Rails.env.development?
      Geocoder.search(request.remote_ip).first
    else
      request.location
    end
  end

on my homepage if I put:

You are in <%= request.location.country %>

it displays locally "you are in Reserved". It think that ip = 00.00.00 and geocoder is built to return "reserved in that case".

Fair enough, if I put : <%= request.remote_ip %>, it then works and gives me the IP i have put in config/environment/develomment.rb: 84.34.156.155

So i tried to put "You are in <%= request.remote_ip.country %>", but it does not work and gives me the error:

undefined method `country' for 84.34.156.155":String

How can I display locally the countriy of the IP location i have set !

Thanks

It works fine in Production (it displays 'you are in France'!) but not locally!

How can I do? It's major that i could work and check stuff locally and not have to deploy everytime to check if it works.


Solution

  • You should call it as

    Geocoder.search(request.remote_ip).first.country