Rails 3.1不重新加载更改的视图

时间:2022-06-12 20:40:57

I'm seeing a bug/feature in my Ruby 1.9.2 applications. Any changes to views (not ruby files) require a server restart. I initially encountered this in a Rails app, but I've tested the same thing in a minimal Sinatra app too.

我在Ruby 1.9.2应用程序中看到了一个错误/功能。对视图(而不是ruby文件)的任何更改都需要重新启动服务器。我最初在Rails应用程序中遇到过这个问题,但我也在最小的Sinatra应用程序中测试了同样的东西。

I'll include a simple app to demonstrate

我将包含一个简单的应用程序来演示

# testapp.rb
require 'sinatra'

get '/' do
  [0,1,2].to_s  #change this to [0,1].to_s
end

This is my procedure:

这是我的程序:

  • ruby testapp.rb (runs thin server for me)
  • ruby testapp.rb(为我运行瘦服务器)

  • load the page
  • 加载页面

  • open the file and edit the view
  • 打开文件并编辑视图

  • reload the page (I see no changes)
  • 重新加载页面(我看不到任何更改)

  • kill the server
  • 杀死服务器

  • restart the server (changes now visible)
  • 重启服务器(现在可见更改)

I've been developing in Ruby 1.8.7 on Rails 3 for the last several months. Having to restart the server on any view change slows down development severely.

在过去的几个月里,我一直在使用Rails 3上的Ruby 1.8.7开发。必须在任何视图更改时重新启动服务器会严重降低开发速度。

I've ready this SO thread, but in my version of Rails (3.1.0 rc4), the config variable is already set as per that answer. Additionally I can replicate the error using Sinatra, so that doesn't seem like it's the case.

我已经准备好了这个SO线程,但是在我的Rails版本(3.1.0 rc4)中,配置变量已根据该答案设置。另外,我可以使用Sinatra复制错误,因此看起来并非如此。

Can anyone shed light on this issue?

任何人都可以解释这个问题吗?

Ruby version: ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-linux] Server: thin 1.2.11 (also tested this with Sinatra/Webrick)

Ruby版本:ruby 1.9.2p180(2011-02-18修订版30909)[x86_64-linux]服务器:瘦1.2.11(也用Sinatra / Webrick测试过)

EDIT 7/13, Clarification The Sinatra thing is a separate issue--Sinatra's source reloading is disabled by default. I used this code to test out the behavior:

编辑7/13,澄清Sinatra是一个单独的问题 - 默认情况下禁用Sinatra的源重新加载。我用这段代码来测试行为:

require 'sinatra'
require 'sinatra/reloader'
require 'haml'

set :views, 'views'

get '/' do
end

get '/test' do
  haml :test
end

With this, I made a file: views/test.haml. Modifying it while the server is running does show a change when the page is reloaded. Thanks to Tiredpixel for pointing this out

有了这个,我创建了一个文件:views / test.haml。在服务器运行时修改它确实在重新加载页面时显示更改。感谢Tiredpixel指出这一点

Unresolved Issue: why does Rails 3.1 on Ruby 1.9.2 not reload the views? I'm able to get ruby files to load, but not haml and erb files. I end up restarting the server just to see if a bug was actually fixed (or not fixed) because of a file not loading properly.

未解决的问题:为什么Ruby 1.9.2上的Rails 3.1没有重新加载视图?我能够加载ruby文件,但不能加载haml和erb文件。我最终重新启动服务器只是为了查看错误是否实际修复(或未修复),因为文件未正确加载。

EDIT/SOLUTION (copied from my comment in the accepted answer):

编辑/解决方案(从我在接受的答案中的评论中复制):

The problem was in config/environments/development.rb

问题出在config / environments / development.rb中

config.cache_classes = false

Even after we checked this was correct, we still had the issue. Further down in the file we had:

即使我们检查了这是正确的,我们仍然有问题。我们在文件中进一步向下:

config.threadsafe!

What this does is set the following 3 flags to true: config.allow_concurrency, config.preload_frameworks, and (surprise!) config.cache_classes.

这样做是将以下3个标志设置为true:config.allow_concurrency,config.preload_frameworks和(surprise!)config.cache_classes。

To fix: move config.threadsafe! above config.cache_classes, so that it doesn't get implicitly overridden.

修复:移动config.threadsafe!在config.cache_classes之上,因此它不会被隐式覆盖。

1 个解决方案

#1


7  

Rails is normally configured to automatically reload on every request, whilst in the development environment. This doesn't happen for files in lib/, though.

Rails通常配置为在开发环境中自动重新加载每个请求。但是,对于lib /中的文件,这不会发生。

The experience you describe with Sinatra is intended (automatic reloading was removed in 0.9.2): http://www.sinatrarb.com/faq.html#reloading; the Shotgun gem can be installed to perform this reloading.

您在Sinatra中描述的经验是有意的(在0.9.2中删除了自动重新加载):http://www.sinatrarb.com/faq.html#reloading;可以安装Shotgun gem来执行此重新加载。

#1


7  

Rails is normally configured to automatically reload on every request, whilst in the development environment. This doesn't happen for files in lib/, though.

Rails通常配置为在开发环境中自动重新加载每个请求。但是,对于lib /中的文件,这不会发生。

The experience you describe with Sinatra is intended (automatic reloading was removed in 0.9.2): http://www.sinatrarb.com/faq.html#reloading; the Shotgun gem can be installed to perform this reloading.

您在Sinatra中描述的经验是有意的(在0.9.2中删除了自动重新加载):http://www.sinatrarb.com/faq.html#reloading;可以安装Shotgun gem来执行此重新加载。