Search…
ENGLISH
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.
1. Install LiveReload extension by accessing the link below. (Chrome browser) https://chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei
2. Open the Gemfile file and add the :development group as shown below.
1
group :development do
2
gem 'guard'
3
gem 'guard-livereload', '~> 2.5'
4
gem 'rack-livereload', '~> 0.3.16'
5
end
Copied!
3. Type the following commands in the terminal.
1
bundle install
2
guard init livereload
Copied!
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 Apply > OK to 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/environments/development.rb
1
config.middleware.insert_after ActionDispatch::Static, Rack::LiveReload, host: 'Second URL', port: 80
Copied!
Enter the rest of the URL except "https://". Make sure the port is 80.
6. Open the Guardfile file and find the content below. (If you don't see the Guardfile file, click the Refresh button at the top right of the Project Explorer and browse again.)
1
guard 'livereload' do
Copied!
Change this to something like this:
1
guard 'livereload', port: 3001 do
Copied!
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.
1
guard
Copied!
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 http in the address bar and try again.
9. At this point, if you see the message INFO-Browser connected on the guard console, it is done successfully.
You can now see that your browser automatically updates whenever there are changes in your files.
Last modified 3mo ago
Copy link