Very often I find myself tired needing to manually refresh my browser after every little change I do to my code. It is not too hard to do it, but sometimes it is just so annoying.
In such cases I like setting up Rails app preview auto re-loadable. It's pretty easy to do and only takes 5 steps:
First off, let's install a couple of gems into development environment, since live-reloading doesn't even make sense on production.
STEP 1: Add the following to your Gemfile:
gem 'guard-livereload'
gem 'rack-livereload'
and run 'bundle install'.
Alternatively, you can run these commands from within your terminal to install the necessary gems
bundle add 'guard-livereload'
bundle add 'rack-livereload'
STEP 2: Next, we need to initialize livereload, by running:
bundle exec guard init livereload
STEP 3: Now add the following line to the very end of file enviroments/development.rb
config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload
STEP 4: Now you can run the command to start the livereload guard. Keep it running alongside your puma server.
bundle exec guard
PS. You may need to restart your Puma server before livereloading starts working.
STEP 5: install 'LiveReload' extension on your browser.
In order to start your autoreloading preview do this:
start your Puma Server
rails start
start your autoreload guard
bundle exec guard
Make sure to do so in the browser with LiveReload extension running.
That's pretty much it. Now every time you save your code-base file it'll auto reload your browser tab.
You may also watch the video about it on my YouTube channel:
Top comments (9)
bundle exec guard
Gives that error ".......lib/guard/livereload/reactor.rb:48:in
_start_reactor': undefined method
epoll?' for EventMachine:Module (NoMethodError)"Which Rails version? I was using 6 here
I used 6.1.4.4 when i tried
did you fix the issue eventually?
No, I gave up. Don't have much experience in such cases.
Same here with Ruby 3.0.3 and Rails 7.0.0.
Not sure you need live reload for Rails 7 though
Man thanks for this tutorial, it was really helpful ! :)
Thanks man....it helped