开发者

How to connect to a localhost Rails site via mobile device locally?

开发者 https://www.devze.com 2022-12-15 13:54 出处:网络
I have a Rails site that I\'m developing on localhost Ubuntu and I have a mobile. My site is running on http://localhost:3000.

I have a Rails site that I'm developing on localhost Ubuntu and I have a mobile. My site is running on http://localhost:3000.

I want to access this directly via my mobile browser not going through the开发者_JAVA技巧 internet.

Is there any way to access it via WiFi or some other way?


If your computer is accessible from internet, just enter in your mobile browser:

http://your.ip:3000/

You could also create a local network (via wifi for instance), connect to it with your cell phone and then do the same thing.

If you are using Rails 4.2+, start the server using:

 rails server -b 0.0.0.0 

(see http://edgeguides.rubyonrails.org/4_2_release_notes.html#default-host-for-rails-server).


1.) get your local ip by typing:

ifconfig |grep inet

into your shell/terminal. your ip usually looks like this: 192.xxx.xxx.xx

2.) then start the rails server with:

rails server -b 0.0.0.0

3.) your application can now be reached by typing:

192.xxx.xxx.xx:3000 into the browser.


Using ngrok

So basically, you run the command

./ngrok http 3000

And you will see the output like that

How to connect to a localhost Rails site via mobile device locally?

And all you need is copy the highlight URL and paste to the browser.


Execute

rails s -b IP_ADDRESS

Access mobile: IP_ADDRESS:3000


  1. First find your Ip and note it down by typing ifconfig terminal(you should be connected to wifi or network)

ifconfig

than run the rails server by typing

rails server -b 0.0.0.0 (YOUR IP )

and press Enter SERVER WILL BE RUNNING ON YOUR_IP:3000 2. Now connect your mobile with the same network and open url

https://YOUR_IP:3000 CONGRATS YOU ARE RUNNING LOCALHOST ON MOBILE


On rails-server machine:

  • Make sure you're running rails-server.
  • Get your IP address, I got "192.168.1.112" for example.

On mobile:

Access through browser by using:

http://<your_ip_address>:3000

http://192.168.1.112:3000

NOTE: Your mobiles need to have the same network with rails-server machine.


To get yourself IP address, run this command in your rails-server machine.

MacOS or Linux Terminal:

ipconfig getifaddr en0 || ipconfig getifaddr en1
# => 192.168.1.112

Description:

  • ipconfig getifaddr en0 is for wifi connections
  • ipconfig getifaddr en1 is for wired connections

Windows:

ipconfig

Then looking for IPv4 Address.


Running rails-server

On rails-server machine, it's okay to regularly run:

bin/rails s

If you found issue, you can try the following:

bin/rails s -b 0.0.0.0

Note: 0.0.0.0 is not an IP address, it's a shortcut for the system binding call to use all available IP addresses including 127.0.0.1, localhost.

Access through http://localhost:3000/


if you get ipconfig command not found on ubuntu Try: ip route to get the IP address then you can run rails server as explained in other answers.

rails s -p 3000 -b 192.168.0.102 (Replace IP with your system's IP).

Now you should be able to access on you mobile browser by just entering http://192.168.0.102:3000/


  • run your server on 0.0.0.0:port_no rather then 127.0.0.1 or localhost

  • then check your IP address by typing a command in your terminal

    ipconfig

    How to connect to a localhost Rails site via mobile device locally?

    here you can check your host machine's IP address.

-Now we have to enable 3000 in your case port to be accessible in the network

write following commands

sudo ufw enable
sudo ufw enable port_no
sudo ufw status

then you will be able to see your port is enabled

-Now you can access your server in any device browser using IP address:port_no

How to connect to a localhost Rails site via mobile device locally?

Check out this picture where I am accessing Django Web App running locally on Ubuntu and I am able to access it on my windows laptop which is in the same network via ip_addess:port_no which in my case is 8000

You can access the same website at

0

精彩评论

暂无评论...
验证码 换一张
取 消