11

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?

Peter Black
  • 1,142
  • 1
  • 11
  • 29

4 Answers4

12

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

notapatch
  • 6,569
  • 6
  • 41
  • 45
Stefan
  • 109,145
  • 14
  • 143
  • 218
  • Rails 6+ you'll find that instead of everything working you instead have a "blocked host" message. [There is a SO question for this too](https://stackoverflow.com/questions/53878453/upgraded-rails-to-6-getting-blocked-host-error) – notapatch Apr 21 '21 at 21:18
  • @notapatch have you tried to add the server’s host name to `config.hosts`? – Stefan Apr 22 '21 at 04:57
  • yes, everything works perfectly once you alter the config.hosts, thanks. My comment was a help to people who after following your excellent answer are left with a "blocked host" and wondering what to do next and if they did anything wrong (obviously, they didn't). A better long term solution would be to edit your answer, but I didn't feel confident enough when I wrote the comment (I was worried I was mistaken). – notapatch Apr 22 '21 at 10:35
  • @notapatch I've updated the answer. Somehow I would've expected Rails to add the host from `-b` automatically to the `config.hosts` array. – Stefan Apr 22 '21 at 10:56
  • Looks good. Yes, I was also confused why there wasn't a generic configuration in the development config. I went with `config.hosts.clear` solution because otherwise everyone on the team would have their own version of the development config. However, I didn't understand the security implications enough to put it in a SO answer. – notapatch Apr 22 '21 at 11:32
7

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.

Bart Jedrocha
  • 11,450
  • 5
  • 43
  • 53
5

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!

Chase
  • 2,748
  • 2
  • 23
  • 32
  • I tried following your instructions, but it tells me to setup ngrok... So i followed the help instructions and in my server log for ngrok: ( GET / 302 Found ) But then it redirects the ipad to ngrok.com – Peter Black Apr 20 '15 at 17:07
  • Im not sure what you changed, but following the instructions always redirects to ngrok.com – Peter Black Apr 21 '15 at 20:13
  • I changed ./ngrok to ./ngrok http 3000, which tells ngrok what to listen to. Are you using http or https? What is your tunnel status? Does your output look like mine? – Chase Apr 21 '15 at 20:21
  • Can you visit the web portal while running ngrok? Go to localhost:4040 and see what's going on. Check the docs here https://ngrok.com/docs/2#expose – Chase Apr 21 '15 at 20:27
  • I talked with the creator for a while.. I (kinda) got it up and running.. But im still getting errors.. Cool program tho! And great response time when asking questions through the page – Peter Black Apr 21 '15 at 21:41
  • Yeah, they're great! What did you have to do to get it working? I'd love to update my answer for future googlers. – Chase Apr 22 '15 at 01:16
0

192.168.1.6 is an ip address, eg the address of your computer on the local network.

:3000 is a port number (well, 3000 is the number, the ":" just separates it from the ip address), in this case the port which Rails runs on.

localhost.dev is an alias set up on your machine, probably for 127.0.0.1 which is the standard "this computer, ie the 'local host'" ip address.

I wouldn't have expected 192.168.1.2.dev to work at all because it's not a valid ip address - it's an ip address with the last bit of your alias on the end.

You should use "192.168.1.2:3000" on your ipad to connect to your local computer's rails server. You shouldn't need to change your proxy settings (so set them back to normal).

If you specifically want to use a particular domain name on your ipad (when you access your rails app), which can be useful if your app displays the site differently based on the domain or subdomain that it was accessed via, then you can use the awesome http://xip.io/ . This is probably more complicated than you need though, it sounds like you just want to see what your rails app looks like on an ipad.

Max Williams
  • 32,435
  • 31
  • 130
  • 197