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

点点圈 提交于 2019-12-08 04:32:44

问题


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.


回答1:


You should call it as

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


来源:https://stackoverflow.com/questions/24216883/geocoder-gem-getting-reserved-for-request-location-country-even-in-local

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!