Connect Ruby/Rails to ipad via local network - ruby-on-rails

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?

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.
Open System Preferences > Sharing to look up your computer's .local name:
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
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
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)
On your iPad / iPhone, open Safari, tap the Tabs icon, scroll down and select the Rails tab:
Done:

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.

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!

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.

Related

why I am not able to access rails localhost on my mobile or other computer

I am trying to access my localhost of rails running project on my mobile device.
This is how I am trying.
http://ip:3000
But it says the webpage is not available.
I tried with another port as well and It doesn't work even there?
What's wrong here? I use to check earlier this way.
Please guide
Try running this code below to your server:
rails s -b 0.0.0.0
This worked for me!
Just give:
ip = your phone's ip address
(Check it in connection information if you are running this on Ubuntu)
ip:3000/your_page if you do not have mapped your root to some page.
Else ip:3000 will work if you have mapped root to some page.
Try it in Chrome and see as it doesn't need any http or https it automatically selects the required one.
Here is how I did it
Put both your computer and phone on the same wifi network
rails s -b 0.0.0.0
routerlogin.net (or however you access your router it will say on the back of router)
get attached devices to the router. Note your computers ip. Mine was 10.0.0.20
on phone go to 10.0.0.20:3000 (but use the ip for your computer from step 4)

Rails application not visible to local network

For the first time since upgrading to OSX Yosemite, I need to view an app running on my machine from another machine on the same network. Previously, this was as simple as finding my internal IP address and using that with port 3000, eg. http://192.168.0.111:3000.
However, I am now finding that with Yosemite this doesn't work. The application is definitely running and is available via localhost:3000 but not via my internal IP.
I have run the network utility port scanner and it shows that localhost exposes port 3000 but my IP doesn't. Other machines on the network that have yet to upgrade (10.7.5 and 10.9.5) are not having this issue.
Any help would be greatly appreciated.
Edit: According to the security and privacy pane of the system preferences, the Yosemite firewall is currently off - so that isn't causing the problem.
By default, rails server will only accept connections from localhost. You can check this by looking at the console output:
Listening on localhost:3000, CTRL+C to stop
To listen on all addresses, which will allow you to connect from other machines on the local network, you must explicitly bind to a more permissive address. Try this:
rails server --binding=0.0.0.0
You should now see:
Listening on 0.0.0.0:3000, CTRL+C to stop
Now you can connect to your Rails app from elsewhere on your local network, by browsing to e.g. http://192.168.0.111:3000.

How do you host a Ruby on Rails application on a local network, so multiple people can access it?

I'm brand new to RoR and have pretty much 0 experience with it. I have been handed down somebody else's project and I need to find a way to host the application, so people can just access it locally. The application is a spider script/walker script/web scraper whatever you call it. Basically it connects to a website, logs in, retrieves certain data each day and maps it with the previous data. While on the local machine, you use it, go to localhost:3000, and you get the webpage the previous person designed. I was just wondering how I could get that page to be public for the people on our local network, so they could connect to some arbitrary IP and see the same page (it updates daily)?
What I tried doing is making the folder containing the application public to the network, but in order to use it, I would have to make everybody on the network allowed to write to it and each person would have to install RoR to use it. I want to avoid that since it doesn't seem logical, nor is it what I'm trying to do.
Looking at the code, I can reverse engineer and understand what it does, but when it comes to hosting web apps locally or something of that sort, I have never done this before. Please help!
Thanks in advance!
**EDIT
-This is all being done on a Windows 7 machine.
Since you're on Windows, open up a command line and run ipconfig to find out your local IP. It will be listed under 'IP Address'.
Tell people in your LAN to access http://192.168.x.x:3000 replacing 192.168.x.x by your IP address from step 1.
EDIT: One major thing that I missed, you are windows. On windows u could use thin and put it behind a load balancer. Although i would suggest hosting it on a UNIX machine :)
Although Running it in webrick(webrick is the application server for development i.e when u run rails s) will let other users access the website NEVER do the same for a production application. If you want to run this application in production, u need more powerful application servers like passenger. I would suggest you use it with Apache or Nginx instead of stand alone passenger. Once all this is setup others can use your application by entering the IP(xx.xx.xx.xx) also u can ask your system admin to setup a local DNS so your users need not remember the IP address always.
Description:
While starting Rails Server, we can also setup some options to configure the IP address and also the port number of the site under development environment to host the website in local network. So if we want to change the IP from http://127.0.0.1:3000 to http://192.168.x.x:port (x= 0 to 255 any one number), we can set that in Rails server command! But for this, we will need to find out our current IP address at our current network which will help us to serve the website in local network.
So at first step:
We will open our terminal on our PC. For Android user, we need to open Termux app. Then simply type ifconfig to get the IP address of our device in the current network. We will get an output like this one (Here I'm using Android device for development. So output might be something different than this one on your PC terminal but the process is same):
$ ifconfig
Warning: cannot open /proc/net/dev (Permission denied). Limited output.
lo: flags=XX<UP,LOOPBACK,RUNNING> mtu XXXXX
inet 127.0.0.1 netmask 255.XXX.XXX.XXX
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen XXXX (UNSPEC)
wlan0: flags=XXXX<UP,BROADCAST,RUNNING,MULTICAST> mtu XXXX
inet 192.168.1.103 netmask 255.XXX.XXX.XXX broadcast 192.168.1.255
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen XXXX (UNSPEC)
X= some numbers with our device credentials which is dummied with this variable
If you are using a WiFi router then we will need the wlan0 part to get our device IP. Under wlan0 section there is a subsection of inet showing our current IP! YUP, we need that IP address 192.168.1.103! This might be different for your device and network. This is the key of this mission! Now we're going to the second important step.
So the Second Step is:
That required input command to configure the IP address.
rails s -b 192.168.1.103 -p 8080
Here:
rails s for rails server
-b 192.168.1.103 for bidding the IP address for customisation from the default IP http://127.0.0.1 which is our localhost address.
-p 8080 for port setup. This part is optional. Default port is 3000.
This is the process of changing the localhost IP (http://127.0.0.1) to local network IP which will be available for other device of the same network user.
Now our rails app is available in our local network! Other users in the same network will also be able to visit the website while the server command is running. And the link will be http://192.168.1.103:8080 if you also configure the port number. Otherwise if you have used the command rails s -b 192.168.1.103 without port configuration the link will be: http://192.168.1.103:3000
Again: 192.168.1.103 was for my case, your IP address will be different for your device. That will be needed to use for your server and link address.

how do i run a development rails app / website on an ipod

I have a app / site that i am running on my local imac and i want to test it on my ipad browser.
Can connect to my imac localhost rails app throught the wifi with my ipad ? If so how!! ?
My app is a multi tenant app that uses subdomains and so i use the hosts file on my my to point for example achme.mycompany.dev to localhost.
How do i do this on the ipad to goto achme.mycompany.dev in the browser and it runs the local app on my imac ?
Hope this all makes sense! Please help.
best regards
Rick
I'm providing an update for anyone who's using Rails 4, has followed the steps in the accepted answer, and still can't connect to the app via development_machine_ip:3000. The reason being that 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 a different device on your 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 development_machine_ip:3000.
I am hoping that your macbook and iPad are on same network. Now go to "Open network preference". Check screenshot.
You will see an ip.
then from ipad open: your_ip:3000
I use proxylocal ngrok for this:
$ ngrok http 3000
ngrok by #inconshreveable (Ctrl+C to quit)
Session Status online
Account Sergio (Plan: Free)
Version 2.2.8
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://be278635.ngrok.io -> localhost:3000
Forwarding https://be278635.ngrok.io -> localhost:3000
Connections ttl opn rt1 rt5 p50 p90
0 0 0.00 0.00 0.00 0.00
Now you can open http://be278635.ngrok.io from your ipad without worrying about your machine's ip address.
If the subdomain is just going to 127.0.0.1:3000 (or whatever) you should be able to access it the same way by going to your machine's local IP address with the same supplied port number. I look at my rails app on my phone all the time.
If you have virtual hosts or something set up, it's probably going to be more complicated than that.
For this, you would either need to jailbreak your iPad to edit the /etc/hosts file, or add a DNS entry in your router (assuming your router is your DNS server).
One of those two sounds like the easiest.

Remotely viewing web pages served by pow.cx

Using WEBrick you could navigate to an app you were serving from another device/virtual machine by navigating to your.ip.address.here:port
Is it possible to do something similar with pow.cx?
The latest version of Pow (0.4.0) now includes xip.io support. You can read about the release here.
Here's a quick explanation of how this helps Pow serve your Rails apps across your entire local network, from their post:
Say your development computer’s LAN IP address is 10.0.0.1. With the
new version of Pow, you can now access your app at
http://myapp.10.0.0.1.xip.io/. And xip.io supports wildcard DNS, so
any and all subdomains of 10.0.0.1.xip.io resolve too.
Here's a description of xip.io, from their site:
xip.io runs a custom DNS server on the public Internet. When your
computer looks up a xip.io domain, the xip.io DNS server extracts the
IP address from the domain and sends it back in the response.
There are basically two options:
Don't use pow: run your applications on localhost as usual and access them as usual
Edit the hosts file (or local DNS) to point your server machine ip
Example accessing from a virtual windows machine:
Suppose you are running two rails applications in pow: store.dev and auth.dev, and you want to access them from a windows xp virtual machine to test them with IE, you only need to edit your hosts file to add the lines:
C:\WINDOWS\system32\drivers\etc\hosts
# Allow to access applications in pow.
# The ip address points to the host machine localhost, that usually is the default gateway
10.0.2.2 store.dev
10.0.2.2 auth.dev
And then open the IE browser to access your applications in http://store.dev and http://auth.dev respectivelly.
Specifically, no, because Pow uses the Host header of the request to determine which app you need to access. To get that working remotely, you would have to have the remote machine map the required domain name to your IP address - either with a local DNS server or by editing the HOSTS file. Both of which are possible but annoying.
The simplest thing to do in that case is to start up a standalone Rails server as you mentioned (using ./script/server or rails s depending on the version), and then you can address http://[ip address]:3000 as before.
In other words, Pow works because it intercepts your local domain resolution, something that isn't affected by (or available to) remote machines.

Resources