Testing on Mobile With Rails

When developing for mobile, you can get most of the way there by changing your viewport width to simulate a mobile device. But sometimes, that’s just not good enough.

Luckily, if you’re working on a Rails app, there’s an easy way to configure your local development environment so you can access it on any mobile device you need to test with.

Note: Before you try this out, make sure you’re on a secure private internet connection. Since your app will be listening on every network interface, you may be vulnerable if you’re on public or untrusted networks.

1. Find Your Internal IP

First, find out what your computer’s internal IP is behind your router. You can find this with ifconfig in the command line, or if you’re on a Mac, you can find it in System Preferences –> Network under the Status field for your wifi connection.


2. Launch Rails & Listen

Next, launch your rails app without binding to your development machine with rails s -binding=0.0.0.0. You can also use rails s -b 0.0.0.0 if the first command gives you trouble.

The -b flag binds the server to a specific IP, but by listening on 0.0.0.0 you allow any incoming connections to hit the app instead of only granting access to your local machine.

1
2
3
4
5
6
7
8
9
10
rails s -b 0.0.0.0

# => Booting Thin
# => Rails 4.1.4 application starting in development on http://0.0.0.0:3000
# => Run `rails server -h` for more startup options
# => Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
# => Ctrl-C to shutdown server
# Thin web server (v1.6.3 codename Protein Powder)
# Maximum connections set to 1024
# Listening on 0.0.0.0:3000, CTRL+C to stop

3. Go To There

Finally, get your device and enter your development computer’s internal IP and the app port in whatever browser you’re testing. Now you can hit your local app as you develop!

Comments