Make Rails 4.2 server listens to all interfaces

on Rails 4.2, Rails, Rails Server, Rails 4.2 Remote Access

After upgrading Rails to 4.2, you cannot access the development mode rails server from another computer/virtual machine (remote access) for testing. So, why can this happen and how to resolve it?

Reason

This is caused by a change in Rails 4.2, the rails s command no longer listens at 0.0.0.0 (means listen on all interfaces) by default, its default is now localhost instead. It means you can only use http://localhost:3000 or http://127.0.0.1:3000 to access rails server, http://192.168.1.123 will not work now.
You can see the difference between Rails 4.1 (and ealier) and 4.2 when starting Rails server:

In Rails 4.1
=> Rails 4.1.9 application starting in development on http://localhost:3000

In Rails 4.2
=> Rails 4.2.0 application starting in development on http://0.0.0.0:3000

Solution

If you need remote access, you can choose one of below methods:

  1. Apply in each start
    Start rails server by appending -b 0.0.0.0, so command to start rails server is: rails s -b 0.0.0.0

  2. Change default binding option
    Modify config/boot.rb by adding below code and start rails server with normal command rails s

require 'rubygems'
require 'rails/commands/server'

module Rails
  class Server
    alias :default_options_bk :default_options
    def default_options
      default_options_bk.merge!(Host: '0.0.0.0')
    end
  end
end

A full stack developer with Ruby on Rails as the main framework. Eager to learn and share.