Rails 4 + Ruby 2 + Geocoder gem
Controller Action
def index
@your_id = request.ip #here i am getting localhost IP address like "127.0.0.1"
binding.pry
end
I think in @your_id , it should capture IP address not localhost IP address("127.0.0.1").
But at view i need to show the city that depends on my location example if you are in NY city then it should show NY city (by ip address) dynamically.
Any idea how to get city_name and IP address?
The geocoder gem provides a #location
method on the request object (source) which returns a location result with latitude and longitude:
request.location
=> #<Geocoder::Result::Freegeoip:0x007f8ecca5d608
@cache_hit=true, @data= {
"ip"=>"199.21.87.210", "country_code"=>"US",
"country_name"=>"United States", "region_code"=>"CA", "region_name"=>"California", "city"=>"Berkeley", "zipcode"=>"", "latitude"=>37.8716, "longitude"=>-122.2728, "metro_code"=>"807", "areacode"=>"510"
}>
To test this functionality in the browser, try using ngrok to expose your local server and throw some debugger statements in your controller to see what the request.location
object looks like.