Live Reloading in Rails
Learn how to live reload your Rails application.
Introduction
When developing the frontend of any application, the most tedious thing is refreshing the client/server. Luckily, when it came to HTML, I had live server; and when it came to Node, I had Nodemon. When it came to ruby, the server refreshed but not the client.
Rails Live Reload
Is a Rails live reloader for views, partials, css, and js. This means that every single time you save a .erb, .css or .js file, the page will reload to show the changes.
Configuration
- In Gemfile add:
Ruby
group :development do
gem "rails_live_reload"
endIn order to only download this gem in development. Then run bundle.
- Create Initializer
Ruby
#Create config/initializers/rails_live_reload.rb and add
RailsLiveReload.configure do |config|
# config.url = "/rails/live/reload"
# Default watched folders & files
# config.watch %r{app/views/.+\.(erb|haml|slim)$}
# config.watch %r{(app|vendor)/(assets|javascript)/\w+/(.+\.(css|js|html|png|jpg|ts|jsx)).*}, reload: :always
# More examples:
# config.watch %r{app/helpers/.+\.rb}, reload: :always
# config.watch %r{config/locales/.+\.yml}, reload: :always
# config.enabled = Rails.env.development?
end if defined?(RailsLiveReload)- Enjoy! With just two steps you saved yourself hours of refreshing the client in Rails!