How to restart rails automatically when code changes.
Have you ever joyfully written some Ruby code in Ruby on Rails, switched over to your browser expectant to see the fruits of your wizardry only to see that the changes are not reflected?
Problem the code you wrote was not reloaded
Well here’s a solution.
Introducing Nodemon.
Nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.
You can use this in rails to solve the same pain point.
Installation
npm install -g nodemon
Add the following file to your project root.
nodemon.json
{
"ignore": [
".git",
"node_modules/**/node_modules"
],
"watch": [
"app/controllers/",
"app/models/",
"app/assets/",
"config/",
"db/"
],
"ext": "rb yml js css scss"
}
Run the command below.
nodemon — exec “rails s”
And voila whenever code changes in the folders specified in nodemon.json
your app will be restarted.
“My app structure is complex!”, no problem, you can add custom paths in the nodemon.json file.
References:
The solution is part of the village knowledge at Kopo Kopo, Inc.