Can a Ruby on Rails project reflect the modifications without restarting the server?
In the Ruby on Rails project, you can reflect your modifications without restarting the server.
After modifying and saving the source code, simply refresh the browser that is accessing the application.
With a few settings, LiveReload can also be applied to modify front-end code without having to refresh the browser. Please follow the steps below.
2. Open the
Gemfilefile and add the
:developmentgroup as shown below.
group :development do
gem 'guard-livereload', '~> 2.5'
gem 'rack-livereload', '~> 0.3.16'
3. Type the following commands in the terminal.
guard init livereload
4. Go to the top menu [Project]> [Running URL and Port] and register the URL. When creating a container, one URL is created by default, so add one more to make it two in total.
When the settings are as shown below, click
OKto close. (You can freely set the URL and port.)
The first URL is the URL that will be used to run the Rails server, and the second URL is used when the LiveReload server communicates with the browser and websockets.
5. Add the content below in the file
config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload, host: 'Second URL', port: 80
Enter the rest of the URL except "https://". Make sure the port is 80.
6. Open the
Guardfilefile and find the content below. (If you don't see the
Guardfilefile, click the Refresh button at the top right of the Project Explorer and browse again.)
guard 'livereload' do
Change this to something like this:
guard 'livereload', port: 3001 do
The port of the second URL is entered in the port section.
Note that LiveReload will occur and modify only when files with extensions are set in the extensions object directly below.
7. Run the LiveReload server by entering the following command in the terminal.
If you see something like
INFO-LiveReload is waiting for a browser to connect, it is successfully done.
8. Run the Rails project by clicking the Run button on the top toolbar. Click the URL to connect. If you are connecting via
https, change the address to
httpin the address bar and try again.
9. At this point, if you see the message
INFO-Browser connectedon the guard console, it is done successfully.
You can now see that your browser automatically updates whenever there are changes in your files.