Rails要求我在每次更改后都重启?

17

我的视图按预期工作;每次我更改内容时,页面会立即反映出来。但是,每当我在控制器模型配置文件中进行更改时,我必须重新启动服务器才能看到更改后的效果。

我使用 rails s -e development 命令启动服务器,并显示以下内容:

  => Booting Puma
  => Rails 4.1.8 application starting in development on http://0.0.0.0:3000
  => Run `rails server -h` for more startup options
  => Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
  => Ctrl-C to shutdown server

我的config/environments/development.rb文件长这样:

  # -*- encoding : utf-8 -*-
  Gvm::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 = true

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

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => 'gmail.com',
  :user_name => '...',
  :password => '...',
  :authentication => 'plain',
  :enable_starttls_auto => true
  }

  config.action_mailer.default_url_options = { :host => "localhost:3000" }
  # Para debug apenas, é melhor que a linha abaixo seja adicionado apenas no ambiente de desenvolvimento
  config.action_mailer.raise_delivery_errors = 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

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

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  end

有什么想法,为什么我每次更改后仍然需要重新启动它?

结论(无解决方案):

最终,看起来这是一个Rails和挂载分区的bug。我的Vagrant VirtualBox VM挂载了一个共享文件夹,在这样做时,Rails无法正确处理客户机和主机之间的时间同步。

虽然我没有得到这个问题的正式确认,但这可能是解释最初问题的原因。


1
你尝试过使用 config.reload_classes_only_on_change = false 吗? - Kiloreux
是的,之前在配置文件中有这个内容。但由于它没有改变任何东西,我将其删除了。 - dmmd
1
你使用Spring还是Zeus? - Alexey Shein
@Kiloreux 你是指 config.reload_classes_only_on_change = false 吗?我又加上了,但还是不行 :( @AlexeyShein 不,我没用spring或zeus。 - dmmd
2
你写的这个文件是 config/development.rb,对吗?你也有 config/environments/development.rb 吗? - basiam
显示剩余6条评论
3个回答

8
请在您的development.rb文件中添加此行。这对我很有用。
config.reload_classes_only_on_change = false

注意: 使用 VirtualBox 进行设置时,我们存在一个非常常见的问题:Rails 问题跟踪

解决方案:由于 Rails 4 中的一些更改,您需要在主机和客户端之间同步时间。


嗯...奇怪,我认为问题出在你的Web服务器puma上。通常Rails使用WEBrick - Kh Ammad
请问您能否确认一下,您是否通过虚拟机使用NFS? - Kh Ammad
我在Virtualbox虚拟机上使用Debian,所有的Rails文件都在挂载的分区上。 - dmmd
这是VirtualBox的问题所在:问题 由于Rails 4的一些更改,您需要在主机和客户端之间同步时间才能使用VirtualBox。 - Kh Ammad
我最终放弃了尝试修复这个问题... 我只是创建了一个全新的vagrant服务器,并在那里创建了我的rails项目。 仍然存在同样的问题。 我不知道发生了什么,放弃了尝试... 虽然感谢你的努力。 - dmmd
显示剩余5条评论

5
Rails 4.1自带spring,这可能导致问题。运行spring stop,然后检查是否还有任何spring进程ps ax | grep spring,如果有,请运行pkill -9 spring。重新启动Rails,查看是否能按预期重新加载。

是的,Spring 给我带来了无尽的麻烦。 - Sergio Tulentsev
我不确定我是否有Spring(至少我没有期望拥有它)。可能没有吧?root@debian:/# spring stop -bash: spring: command not found - dmmd
查看您项目的 bin 文件夹,应该有一个名为 spring 的文件。您可以通过 bin/spring 运行它。 - Alexey Shein
bin文件夹内没有Spring。 - dmmd

0

请检查您的development.rb文件,可能存在问题。

config.cache_classes = true

在开发环境中,每次请求时都会重新加载应用程序的代码。这会减慢响应时间,但对于开发来说非常完美,因为您无需在进行代码更改时重新启动 Web 服务器,只需将其设置为 false 即可。
  config.cache_classes = false

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