Rails changed the default behavior for WEBrick somewhere around version 4. Instead of binding to 0.0.0.0
, it will now default to localhost
.
This makes life difficult when you're running Rails inside a VM like Vagrant, mostly because it won't work. ;)
Fortunately, you can force Rails back into the old universal address with the following snippet
# config/boot.rb
# ... end of existing file
require 'rails/commands/server'
# Enforce WEBrick classic port and host
module Rails
class Server
def default_options
super.merge(Host: '0.0.0.0', Port: 3000)
end
end
end
This way you can continue to simply call rails server
without having to addthe extra -b
parameter.
Be sure to leave the port assignment in there; our tests indicate that changing the default host causes the port to break in weird ways (we got a default port assignment of 9292
).
If you put the default options on
config/boot.rb
then all command attributes for rake and rails fails (example:rake -T
orrails g model user
)! So, append this tobin/rails
after linerequire_relative '../config/boot'
and the code is executed only for the rails server command:The
bin/rails
file loks like this:See http://stackoverflow.com/a/32476858/132235