Rails服务器每次修改后都需要重启?为什么?

49
每次我在控制器或模型中更改任何内容,我都必须重新启动服务器才能生效。但这并不总是这样,在之前它可以正常工作,当我更改任何内容时,但现在不知道发生了什么?
我的Rails版本是3.2.11 在我的开发环境文件中,我设置了config.cache_classes = false。 请帮忙解决。
我的development.rb文件如下:
Testapp::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

  # Log error messages when you accidentally call methods on nil.
  config.whiny_nils = true

  # Show full error reports and disable caching
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # 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

  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin

  # Raise exception on mass assignment protection for Active Record models
  config.active_record.mass_assignment_sanitizer = :strict

  # Log the query plan for queries taking more than this (works
  # with SQLite, MySQL, and PostgreSQL)
  config.active_record.auto_explain_threshold_in_seconds = 0.5

  # Do not compress assets
  config.assets.compress = false

  # Expands the lines which load the assets
  config.assets.debug = true

  config.action_mailer.default_url_options = { :host => 'localhost:3000' }

end

是的,@HassanJaveed 是正确的,它应该可以在不提供环境文件的情况下工作。在开发模式下,每次重新加载都是默认行为。 - dirtydexter
1
请发布您的完整环境文件,还有其他因素可能会影响重新加载行为,例如config.threadsafe!。 - Martijn
谢谢您的回复,我已经检查过了,我的服务器正在运行开发模式。 - Amit Sharma
你是如何运行开发服务器的?是在像Pow或Passenger这样的环境下通过rails s命令吗? - Doon
2
对于使用 Vagrant/virtualbox 的用户,如果主机时钟和客户机时钟不同步,就会导致 Rails 的重新加载器出现故障。https://github.com/rails/rails/issues/16678 - Ninjaxor
显示剩余5条评论
5个回答

69

我已经得到答案了。

在我的config/environments/development.rb文件中添加以下行后,我的问题已得到解决。

config.reload_classes_only_on_change = false

有人能告诉我使用这个的缺点是什么吗? - Balaji Radhakrishnan
1
Rails在每个请求中加载类。在生产环境中,您希望这尽可能快,因此通常会配置Rails缓存这些类。在开发中,您对速度不太关心,因此通常禁用缓存机制,以便在下一个请求中考虑文件中的每个更改。这可以避免每次想要测试更改时都必须重新启动服务器。 - jfc
@BalajiRadhakrishnan 当我使用这个功能时,我注意到有时候activeadmin没有及时加载其类以供浏览器使用(或者类似的情况),导致出现错误,例如“undefined method 'namespace' for nil:NilClass”。刷新页面总是可以解决这个问题。 - AFOC

9

在控制台中使用以下命令启动您的服务器

rails server -e development

如果没有启动,请提供您的Rails版本和用于运行Rails应用程序的服务器。

更多配置

修改您的config/environments/development.rb文件为:

config.serve_static_assets = false

谢谢您的回答,我已经尝试了上述命令,但对我没有效果。我的Rails版本是3.2.11,我在我的应用程序中使用Webrick服务器。 - Amit Sharma
请修改您的config/environments/development.rb文件: config.serve_static_assets = false - jayesh
感谢您的回复,问题已经解决了。我在development.rb文件中添加了config.serve_static_assets = false - Amit Sharma
1
解决方案对我有效,但一个小时后同样的问题又出现了。 - Amit Sharma

8
在虚拟化环境中,如果文件在主机操作系统上进行编辑,而客户操作系统的文件事件管理器不会生成有关文件更改的事件,则可能会出现额外的情况。
解决此问题的方法是注释掉以下行:config/environments/development.rb
# 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

因此,得到:
# 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

这将强制Rails实际检查文件修改时间,而不是期望获得文件系统事件。

答案 https://dev59.com/96vka4cB1Zd3GeqPyslW#50640007 提示,你也可以使用另一种文件监视器,例如FileUpdateChecker。 - morgler
在使用Docker时,这可以解决问题。 - Mhmd Az

5

对于Vagrant / VirtualBox用户,用户Ninjaxor发布的评论中有一个好消息:

如果主机时钟和客户机时钟不同步,则存在错误,这将损坏Rails的重新加载器。 https://github.com/rails/rails/issues/16678

您可以在类似以下目录中找到文件Vagrantfile: .../ruby/gems/sass-3.4.22/vendor/listen

在那里,您需要添加以下内容:

# Sync time every 5 seconds so code reloads properly
config.vm.provider :virtualbox do |v|
  v.customize ["guestproperty", "set", :id, "--timesync-threshold", 5000]
end

感谢 GitHub 上用户 axsuul 的贡献!


4
我注意到设置中的
config.cache_classes = false 

这对我起到了积极的作用,让我受益匪浅。

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