Get real IP address in local Rails development environment

前端 未结 8 604
眼角桃花
眼角桃花 2020-11-30 23:15

I have Rails 2.3.8, Ruby 1.8.7, Mongrel Web Server and MySQL database.

I am in the development mode and I need to find the real IP address

8条回答
  •  星月不相逢
    2020-11-30 23:36

    As far as I can see there is no standard method for getting the remote address of your local machine. If you need the remote address for (testing) your geocoding, I suggest adding 127.0.0.1 to your database table that matches IP addresses with locations.

    As a last resort you could also hard code your remote address. E.g. like this:

    class ApplicationController < ActionController::Base
      def remote_ip
        if request.remote_ip == '127.0.0.1'
          # Hard coded remote address
          '123.45.67.89'
        else
          request.remote_ip
        end
      end
    end
    
    class MyController < ApplicationController
      def index
        @client_ip = remote_ip()
      end
    end
    

提交回复
热议问题