Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geocoder, how to test locally when ip is 127.0.0.1?

I can't get geocoder to work correct as my local ip address is 127.0.0.1 so it can't located where I am correctly.

The request.location.ip shows "127.0.0.1"

How can I use a different ip address (my internet connection ip) so it will bring break more relevant data?

like image 451
Blankman Avatar asked May 24 '11 19:05

Blankman


2 Answers

For this I usually use params[:ip] or something in development. That allows me to test other ip addresses for functionality and pretend I'm anywhere in the world.

For example

class ApplicationController < ActionController::Base
  def request_ip
    if Rails.env.development? && params[:ip]
      params[:ip]
    else
      request.remote_ip
    end 
  end
end
like image 169
jesse reiss Avatar answered Sep 29 '22 13:09

jesse reiss


A nice clean way to do it is using MiddleWare. Add this class to your lib directory:

# lib/spoof_ip.rb

class SpoofIp
  def initialize(app, ip)
    @app = app
    @ip = ip
  end

  def call(env)
    env['HTTP_X_FORWARDED_FOR'] = nil
    env['REMOTE_ADDR'] = env['action_dispatch.remote_ip'] = @ip
    @status, @headers, @response = @app.call(env)
    [@status, @headers, @response]
  end
end

Then find an IP address you want to use for your development environment and add this to your development.rb file:

config.middleware.use('SpoofIp', '64.71.24.19')
like image 20
Sean Rucker Avatar answered Sep 29 '22 12:09

Sean Rucker