Get own IP address

Viewed 16145

How to get my own IP address with Rails?

When I do it like this I got: 127.0.0.1

@ip = request.remote_ip

Is there any way to get the Public IP?

6 Answers

This does not answer this question. I think someone else can find this answer helpful.

Problem:

I am developing a mobile app. So, When I debug/hot reload/live reload the app in real mobile device. Images url does not work with localhost:3000.

Images works with ip like this: http://192.168.0.102:3000/user/1/profile-223hfkj33.jpg

Problem is Every time I turn on laptop and connect to wifi router, laptop ip changes. So, every-time I need to change asset_host in environments/development.rb file.

Looking at previous answers I found a solution:

Solution:

in environments/development.rb I write this code:

server_address = "#{Socket.ip_address_list.detect(&:ipv4_private?).try(:ip_address)}:3000"
config.asset_host = server_address

puts "Server address: #{server_address}"
# when I run `rails s`, this line prints server address in console

So, It sets asset_host like this: 192.168.0.102:3000
And when I turn on laptop, laptop gets new ip address and it works.

Related