我正在Vagrant虚拟机上运行准系统Rails 5应用程序。我只生成了一个名为Blog的脚手架,它正在工作。
对类的任何手动更改都需要重新启动Rails服务器才能生效,尽管处于开发模式。
编辑:在尝试了许多其他有缺陷的解决方案之后,对于那些想跳过的人,我已经发布了一个对我有用的答案……
所有Blog脚手架文件均已成功创建,我可以使用索引,编辑等路线查看页面。但是,如果我将@blogs = Blog.all
中的index方法中的BlogsController
更改为@blogs = Blog.limit(1)
并刷新浏览器,则视图不会改变。所有博客文章仍然列出。
我查看了本地目录,然后查看了虚拟机目录。他们匹配。因此更改已在服务器上。
我以@blogs = Blog.limit(1)
作为索引方法的内容关闭并重新启动了Rails服务器,并且页面按预期加载,并出现了一篇博客文章。
如何在不重新启动服务器的情况下使Rails应用程序显示更新的内容?默认情况下是否存在一些可以关闭的缓存?我使用RubyMine作为我的IDE,但是我没有适当的部署设置,也没有打开任何特殊的缓存。我在另一个Rails项目(运行Ruby 2.0.0和Rails 4.1.4)上使用了RubyMine和Vagrant却没有这个问题。
发布版本5.2.3
Ruby版本2.4.1p111或2.6.3p62 (均无效)
RubyMine版本2019.1
Vagrant正在使用VirtualBox并运行 Ubuntu 18.04.2 LTS
根据其他帖子,这是生产环境的默认行为。我在命令行上尝试了echo $RAILS_ENV
,但返回了null。
我在{... 1blogs / index.html.erb中添加了<%= Rails.env %>
,它在浏览器中显示为“开发中”。因此,应用程序环境设置正确,但仍在缓存类(或其他东西)。在Rails 5上是否存在一些东西,即使它在Rails.env变量中显示“开发”,它仍默认将虚拟机视为远程(生产)服务器?
以下是启动Rails服务器后的相关控制台输出:
=> 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
我尝试过的事情:
我已经清除了浏览器缓存并运行rake tmp:cache:clear
。没有
效果。
在“ config / environments / development.rb”文件中, 'config / environments / test.rb',和 'config / environments / production.rb'我设置了以下属性:
config.cache_classes = false
config.action_controller.perform_caching = false
并重新启动Rails服务器。没有效果。
我已从2.4.1p111切换到Ruby版本2.6.3p62。没有效果。
我关闭了RubyMine,并在SublimeText中编辑了'blogs_controller.rb'。没有 效果。
我编辑了“ app / views / blogs / index.html.erb”并刷新了浏览器 窗口而不重新启动服务器,并且对视图所做的更改是 立即反映出来。因此,这不是缓存一切的问题。
将以下内容添加到我的Vagrant文件中,以加强同步来宾和主机时钟并重新更新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
没有效果。
config.vm.network "private_network", type: "dhcp"
config.vm.synced_folder ".", "/vagrant", type: "nfs", mount_options: ['actimeo=1']
没有效果。
v.customize ["setextradata", :id, "VBoxInternal/Devices/VMMDev/0/Config/GetHostTimeDisabled", 0]
没有效果。
这似乎是一个类似的问题,其中op在Mac OS X上使用Docker:Dockerized Rails 5 RC1 application not picking up updates to controllers and models in development,但是我没有使用任何特殊的部署或配置,而只是使用Vagrant vm。
这是另一个类似的问题,但是Rails 5:Rails Server needs restart every time I make changes? why?中不存在接受的答案中的配置设置。其中一条评论提到了一个老问题,即Vagrant来宾和主机时钟不同步可能会破坏Rails的重新加载。要研究这种可能性。 编辑:调整了VirtualBox的时钟同步设置,确认来宾和主机上的时钟同步(当我在每台计算机上运行date
时,它们在3秒钟之内-似乎仍然关闭,但是它们关闭)。没用。
答案 0 :(得分:0)
在另一个线程上找到了答案:https://stackoverflow.com/a/36616931/1512790
无论我对时钟同步,文件类型等进行了什么更改,ActiveSupport::EventedFileUpdateChecker
均未检测到文件更改。
解决方案是:在“ config / environments / development.rb”中,替换此行
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
与此:
config.file_watcher = ActiveSupport::FileUpdateChecker
无论出于何种原因,FileUpdateChecker类在EventedFileUpdateChecker不起作用的情况下都可以在vm环境中使用。
为此解决方案大声喊叫pocari。