I'm running a bare-bones Rails 5 app on a Vagrant virtual machine. I've generated just one scaffold called Blog and it's working.
Any manual changes to the classes, however, require a Rails server restart in order to take effect, despite being in development mode.
Edit: I've posted an answer to this that worked for me, after trying a lot of other flawed solutions, for those who want to skip ahead...
The Blog scaffold files were all created successfully, I can view the pages using the index, edit, etc. routes. But if I change @blogs = Blog.all
in the index method in BlogsController
to @blogs = Blog.limit(1)
and refresh the browser, the view doesn't change. All blog posts are still listed.
I looked in my local directory and the one on the vm. They match. So the changes are on the server.
I closed and restarted the Rails server with @blogs = Blog.limit(1)
as the index method content, and the page loads as expected, with one blog post appearing.
How can I get the rails app to display updated content without restarting the server? Is there some caching going on by default that I can switch off? I'm using RubyMine as my IDE, but I don't have deployment settings in place and I haven't turned on any special caching. I've used RubyMine and Vagrant on another Rails project (running Ruby 2.0.0 and Rails 4.1.4) without this issue.
Rails version 5.2.3
Ruby version 2.4.1p111 or 2.6.3p62 (neither worked)
RubyMine version 2019.1
Vagrant is using VirtualBox and running Ubuntu 18.04.2 LTS
According to other posts, this is the default behaviour for production environments. I tried echo $RAILS_ENV
on the command line, but got back null.
I added <%= Rails.env %>
to '...blogs/index.html.erb' and it shows 'development' in the browser. So the app environment is set correctly but it's still caching the classes (or something). Is there something about Rails 5 where it's treating the vm as a remote (production) server by default even though it's displaying 'development' as the Rails.env variable?
Here's the relevant console output after starting the rails server:
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.4.1-p111), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Things I've tried:
I've cleared the browser cache and run
rake tmp:cache:clear
. No effect.In the files 'config/environments/development.rb', 'config/environments/test.rb', and 'config/environments/production.rb' I set these attributes:
config.cache_classes = false
config.action_controller.perform_caching = false
and restarted the rails server. No effect.
I've switched to Ruby version 2.6.3p62 from 2.4.1p111. No effect.
I closed RubyMine and edited 'blogs_controller.rb' in SublimeText. No effect.
I edited 'app/views/blogs/index.html.erb' and refreshed the browser window without restarting the server and the changes to the view are reflected immediately. So it's not an issue of caching everything.
Added the following to my Vagrantfile in order to tighten the settings that sync the guest and host clock, and re-upped vagrant:
config.vm.provider 'virtualbox' do |v|
v.customize ["guestproperty", "set", :id, "--timesync-set-threshold", 5000] # Sync time every 5 seconds so Rails code reloads properly
v.customize ["guestproperty", "set", :id, "--timesync-interval", 10000] # Specifies interval at which to synchronize time with the host. Default is 10000ms (10 seconds).
v.customize ["guestproperty", "set", :id, "--timesync-min-adjust", 100] # The minimum absolute drift value measured in milliseconds to make adjustments for. The default is 1000 ms on OS/2 and 100 ms elsewhere.
v.customize ["guestproperty", "set", :id, "--timesync-set-on-restore", 1] # Set the time after the VM was restored from a saved state when passing 1 as parameter. This is the default.
end
No effect.
- Reinstalled Ruby, Rails, and all other gems. No effect.
- In Vagrantfile, Changed the file type for the synced folders that vagrant is using and set up a dhcp private network connection:
config.vm.network "private_network", type: "dhcp"
config.vm.synced_folder ".", "/vagrant", type: "nfs", mount_options: ['actimeo=1']
No effect.
- In Vagrantfile, ensured that the guest was getting the host clock data:
v.customize ["setextradata", :id, "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled", 0]
No effect.
This seems to be a similar issue, where the op is using Docker on Mac OS X: Dockerized Rails 5 RC1 application not picking up updates to controllers and models in development, but I'm not using any special deployment or provisioning, just a Vagrant vm.
This is another similar issue, but the configuration setting in the accepted answer doesn't exist in Rails 5: Rails Server needs restart every time I make changes? why?. One of the comments mentions an old issue where Vagrant guest and host clocks being out of sync could wreck Rails reloading. Going to investigate that possibility. Edit: Tweaked the VirtualBox clock sync settings, confirmed that the clock on the guest and host were in sync (they're within 3 seconds when I run date
on each machine - which still seems off, but they're close). Didn't work.