Reputation: 6684
I have a rails form and am trying to capture a client's IP address and ultimately convert that into a zip code. I've done the following:
Controller
def create
...
begin
response = open('https://jsonip.com/').read
data = JSON.parse(response)
ip_address = data['ip']
ip_type = 'jsonip'
rescue
ip_type = 'request.remote_ip'
ip_address = request.remote_ip
end
if ip_address
zip = Geocoder.search(ip_address)
p "IP Address (#{ip_type}): #{ip_address}, zip: #{zip}"
@potential_client.zip_code = zip.first.try(:postal) if zip.present?
end
...
end
This code came from here because request.remote_ip
kept returning the same IP address. It seemed to work but once I push to Heroku, it seems like everyone is still coming from the same IP address.
What else am I missing?
Upvotes: 0
Views: 82
Reputation: 684
With the call to jsonip.com, that is your server rendering and requesting that page so it makes sense that the server IP is returned.
With Heroku and other SaaS providers the remote IP will return the IP of the server or load balancer. Heroku does provide a header "x-forwarded-for" that has a list of IPs the request has passed through.
Upvotes: 1