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

问题描述 投票:0回答:6

我注意到我的 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
ruby-on-rails vagrant puma ruby-on-rails-5
6个回答
101
投票

将以下内容添加到

config/environments/development.rb

#config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.file_watcher = ActiveSupport::FileUpdateChecker

FileUpdateChecker
将通过轮询文件的更改来检测。


21
投票

我已经解决了我的问题,将以下行添加到

development.rb
文件中。

config.reload_classes_only_on_change = false

3
投票

这对我的 ruby 2.6.5 和 Rails 5.2.4.1 有用:

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

config.file_watcher = ActiveSupport::EventedFileUpdateChecker

一起
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
,它将运行所有三个命令,然后您就知道所有内容都已加载。 我也使用这种方法来重复此操作,就像 devise 等 gem 的命令行安装一样。


0
投票

在 Rails 7.2 上遇到这个问题

实施 resque 和 resque-pool 后,我在用于工作分叉的开发和生产文件中有了

config.cache_classes = true

在development.rb中删除这个

config.cache_classes = true
解决了问题

© www.soinside.com 2019 - 2024. All rights reserved.