Is there a way of automatically launching your browser and pointing it to http://localhost:3000
when you run rails server
?
And secondly ensuring this only happens in development?
I would have thought that Launchy would have done the trick, but I'm struggling to work out where to put it.
I've tried adding an initialiser config/initializers/launchy.rb
require 'launchy'
Launchy.open("http://localhost:3000")
And this triggers the browser to open, create a new tab and visit http://localhost:3000
as expected, however it runs before the server / application has finished booting and I get a Cannot connect to Server error in the browser.
If I reload in the browser my app works just fine so I'm confident it's not a problem with my app, rather the timing of the Launch.open
call.
I tried renaming launchy.rb
to z_launchy.rb
so it gets loaded last as per the docs but still the same problem. Launchy fires before the Application is ready for it.
I've also tried adding the code to config/puma.rb
(I'm using puma as my server), to config/enviroments/developoment.rb
but always the same problem. The Launchy.open
command gets called to soon.
Where should I call Launchy.open("http://localhost:3000")
to ensure that a) It runs after the application has loaded and is ready to receive requests; and b) So it only runs in development, not in test or production?
System set up OS X 10.11.3, Rails 4.2.5, ruby 2.2.1p85, puma version 2.15.3
Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .
To generate a controller and optionally its actions, do the following: Press Ctrl twice and start typing rails g controller. Select rails g controller and press Enter . In the invoked Add New Controller dialog, specify the controller name.
Normally in your terminal you can try Ctrl + C to shutdown the server. And this will go back into the process, and then quit out of Rails s properly.
Putting it into config.ru
would work, after the run Rails.application
line. By the time that returns, your app is ready to go. To run it only in development
, check the Rails.env
.
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
Launchy.open("http://localhost:3000") if Rails.env.development?
This is easy, but it feels dirty to me to have this sitting in your app. Better would be a small script that starts your server, waits a moment, then launches the browser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With