Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect Ruby/Rails to ipad via local network

host ip: 192.168.1.2

ipad ip: 192.168.1.3

when working on my host, the web address is: localhost.dev:3000/sign_in/ When I try to connect to my ipad via 192.168.1.2.dev:3000/sign_in the connection times out.

One method to overcome my issue was to change the wifi settings on the ipad to manual proxy:

server: 192.168.1.2

port: 3000

Now 192.168.1.2.dev works! Kinda...

I can now log in to my site, but js seems to be broken and the ipad changes the url to: ( http://2.dev/page ) Normally it would be ( http://localhost.dev:3000/page ) Which I believe is the last digit of the host lan ip. We use some externally hosted js files. Which may be why it is breaking. I can NOT browse the internet (on ipad) while the proxy is enabled. What am I missing?

(192.168.1.2:3000 does not work for any device..)

After further debugging I believe it is because of internet connectivity through the proxy.

The exact error my log spits out is:

 CONNECT configuration.apple.com:443 HTTP/1.1
 Host: configuration.apple.com
 User-Agent: ubd/289.3 CFNetwork/672.1.14 Darwin/14.0.0
 Connection: keep-alive
 Proxy-Connection: keep-alive

 2015-04-20 11:52:54] ERROR TypeError: can't dup NilClass
 /home/pete/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httprequest.rb:279:in `dup'

So I think I have to enable linux to forward my internet as well?

like image 922
Peter Black Avatar asked Apr 20 '15 15:04

Peter Black


3 Answers

I'm not sure what Ruby on Rails version you're on but somewhere around version 4, Rails changed the default binding address from 0.0.0.0 to only localhost. By default this allows you to only access the Rails app via localhost:3000 or 127.0.0.1:3000 - which is fine in most cases.

Now if you'd like to access the app from an iPad on your local network (or any device on your local network) you can use the -b option when starting the server and specify a binding address of 0.0.0.0

rails s -b 0.0.0.0

This will bind on ALL interfaces including localhost and the IP assigned by your network. You should now be able to access the app via your iPad. Hope this helps.

like image 59
Bart Jedrocha Avatar answered Nov 04 '22 21:11

Bart Jedrocha


Rails is not accessible, because the server binds to localhost.

Here's my usual workflow using zeroconf / Bonjour and Mac OS X, although it should basically work with another OS and / or a fixed IP address, too.

  1. Open System Preferences > Sharing to look up your computer's .local name:

    System Preferences > Sharing

  2. Add the host name to config/environments/development.rb so you access the server by that name, e.g.: (required for Rails 6+ see: Blocked host Error)

    Rails.application.configure do
      # ...
      config.hosts << 'stefans-mac.local'
      # ...
    end
    
  3. Start rails server with the -b option to provide the host name (this is the important part):

     $ rails s -b stefans-mac.local
     => Booting Thin
     => Rails 4.2.1 application starting in development on http://stefans-mac.local:3000
     => Run `rails server -h` for more startup options
     => Ctrl-C to shutdown server
     >> Thin web server (v1.5.1 codename Straight Razor)
     >> Maximum connections set to 1024
     >> Listening on stefans-mac.local:3000, CTRL+C to stop
    
  4. Assuming you've enabled iCloud Tabs, visit the URL on your Mac using Safari (so you don't have to enter the address manually on your iPad)

  5. On your iPad / iPhone, open Safari, tap the Tabs icon, scroll down and select the Rails tab:

iPhone

  1. Done:

Safari

like image 42
Stefan Avatar answered Nov 04 '22 23:11

Stefan


If I were you, I would try Ngrok. that way you can temporarily and securely expose your dev machine's localhost:3000 to the ipad. Download ngrok to your application's folder and unzip /path/to/ngrok.zip.

For me that looks like: unzip ngrok_2.0.16_darwin_amd64.zip

Then run rails s.

Finally ./ngrok http 3000.

This command will give you an address to hit from your ipad. When you're done just kill ngrok with ctrl+c.

I get the following output:

NAME:
ngrok - tunnel local ports to public URLs and inspect traffic

ngrok by @inconshreveable                                                                                              (Ctrl+C to quit)

Tunnel Status                 online
Version                       2.0.16/2.0.15
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://014da213.ngrok.io -> localhost:3000
Forwarding                    https://014da213.ngrok.io -> localhost:3000

Connnections                  ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00

Type the forwarding address into your ipad. For me, http://014da213.ngrok.io

No config required!

like image 5
Chase Avatar answered Nov 04 '22 21:11

Chase