Rails服务器无法看到代码更改并重新加载文件。

26

我注意到我的rails服务器在我更改控制器、模型和可能的其他文件后不会重新加载它们。我使用Vagrant和Rails API,我发现一些人通过将以下行添加到Vagrantfile来解决这个问题。

config.vm.provider "virtualbox" do |vb|
  vb.customize [ "guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 5000 ]
end

我并没有解决这个问题。我已经不知道还需要怎样做来解决这个问题了。我会附上这些文件,这可能对你有所帮助。

我的 Gemfile 如下所示:

source 'https://rubygems.org'

gem 'rake', '< 11.0'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '>= 5.0.0.beta3', '< 5.1'
# Use mysql as the database for Active Record
# gem 'mysql2', '>= 0.3.18', '< 0.5'

# User PostgreSQL as the database for Active Record
gem 'pg', '~> 0.18'

gem 'active_model_serializers'

gem 'rspec-its'

gem 'database_cleaner'

# Use Puma as the app server
gem 'puma'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.0'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Gem allowing using external APIs
gem 'httparty'

# Auth0 gem for authentication using JWT
gem 'knock'

gem 'jwt'

# OpenID Omniauth gem for authenticating Steam users
gem 'omniauth-steam'

# Gem for managing environment variables
gem 'figaro'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
gem 'rack-cors', :require => 'rack/cors'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug'
  gem 'rspec-rails', '~> 3.0'
  gem 'factory_girl_rails'
  gem 'ffaker'
end

group :test do
  gem 'shoulda-matchers'
  gem 'json-schema'
end

group :development do
  gem 'listen', '~> 2.10'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

我确定我的服务器在开发模式下运行,因为日志的开头如此

=> Booting Puma
=> Rails 5.0.0.beta3 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma starting in single mode...
* Version 3.1.0 (ruby 2.2.3-p173), codename: El Niño Winter Wonderland
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000

这是我的development.rb文件。
Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports.
  config.consider_all_requests_local = true

  # Enable/disable caching. By default caching is disabled.
  if Rails.root.join('tmp/caching-dev.txt').exist?
    config.action_controller.perform_caching = true

    config.action_mailer.perform_caching = false

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      'Cache-Control' => 'public, max-age=172800'
    }
  else
    config.action_controller.perform_caching = false

    config.action_mailer.perform_caching = false

    config.cache_store = :null_store
  end

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load


  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  # Use an evented file watcher to asynchronously detect changes in source code,
  # routes, locales, etc. This feature depends on the listen gem.
  config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end

我的Vagrantfile

Vagrant.configure(2) do |config|

  if Vagrant.has_plugin?("vagrant-timezone")
    config.timezone.value = "Europe/Warsaw"
  end

  config.vm.box = "ubuntu/trusty64"

  config.vm.network :forwarded_port, guest: 3000, host: 3000
  config.vm.synced_folder "E:/Projekty - Rails", "/home/projekty"

  config.vm.provider "virtualbox" do |vb|
    vb.customize [ "guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 5000 ]
  end
end

在 development.rb 文件中,你能否添加 config.threadsafe! 行?请检查它是否有效? - Mohammad Shahadat Hossain
@MohammadShahadatHossain 当我尝试启动服务器时,它会导致错误。我使用Rails API,所以这可能是原因,对吗? - mparkitny
你能展示一下你的 Vagrantfile 文件吗?我认为问题可能出在那里。 - Mohammad Shahadat Hossain
@MohammadShahadatHossain 添加了 Vagrantfile - mparkitny
@MohammadShahadatHossain 我总是使用这个命令来启动服务器。 - mparkitny
显示剩余2条评论
5个回答

99
将以下内容添加到 config/environments/development.rb 文件中。
#config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.file_watcher = ActiveSupport::FileUpdateChecker

FileUpdateChecker 通过轮询文件变化来检测。


我的development.rb已经修改,现在运行得非常好 - 谢谢 - Hannes
我也试过了,对我也有效。我已经苦恼了两个多星期,一直以为是Rails 5 beta的一个bug。谢谢! - Abdulaziz
4
你好,Rev3rse。使用Virtual Box的共享文件夹时出现了问题,因为虚拟机中的操作系统无法检测到共享文件夹中的文件变更事件。请问有什么解决方法吗? - pocari
1
我也使用了一个共享文件夹。没有使用Vagrant,只是将Windows 10作为主机,Ubuntu 16.04作为客户机。以前当我在Windows中更新文件时,Rails无法看到更改。提出的解决方案很有帮助,谢谢。 - Zhenya
这对我来说在2021年刚刚起作用,但是顶部被注释掉的那行代码已经运行了几周。很奇怪为什么突然出现了这个问题。 - DNorthrup
显示剩余2条评论

19

我通过在development.rb文件中添加以下行解决了我的问题。

config.reload_classes_only_on_change = false

1
由于某种原因,这使得我的 Puma 服务器变得非常缓慢,并开始破坏我的 Devise 用户会话路由。很奇怪。我会再试一次,看看是否仍然会发生这种情况。 - Jay Killeen
是的,它给了我一个Read error:#<ActiveRecord :: ConnectionTimeoutError:在5.000秒内无法从池中获取连接(等待了5.000秒);所有池化的连接都在使用>问题。不确定它是由更改引起的还是我做错其他事情的副作用。 - Jay Killeen
1
我怀疑你会发现它很慢,因为它可能重新加载所有类,而不仅仅是更改的类,并且在每个请求上都这样做。 - Adam Sanderson
4
如果config.cache_classes为true,该选项将被忽略。 - Kick Buttowski
经过这么长时间,这终于在CentOS和Ubuntu上为我解决了问题。 - Jan Wiemers
显示剩余2条评论

3

这对我来说在ruby 2.6.5和Rails 5.2.4.1中有效:

在config/environments/development.rb中添加以下行:

config.file_watcher = ActiveSupport::EventedFileUpdateChecker

along with

config.cache_classes = false

在同一个文件中,以及。
gem 'listen'

在Gemfile中的:development组。

2

pocari的解决方案对我有用,但我必须等待几秒钟才能重新加载页面,否则内容并不总是更新。

按照这个答案中描述的方法,在synced_folder中添加一个选项即可解决问题:

config.vm.synced_folder ".", "/vagrant", type: "nfs", mount_options: ['actimeo=1']

(而且不需要更改development.rb文件)

0

我遇到了同样的问题,所以我做了一个快速脚本 bump,像这样。确保你先进入你的应用程序文件夹。

!#/bin/bash

rake db:migrate
echo "MIGRATED"
rake routes
echo "routed"
sudo service apache2 restart
echo "web server reloaded"

现在您只需键入 ./bump,它将运行所有三个命令,然后您就知道一切都已加载。我也使用这种方法来重复此类安装命令行的 gem,例如 Devise。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接