在环境配置文件中出现了未初始化的常量ActiveSupport::EventedFileUpdateChecker。

5

我刚开始接触Ruby on Rails。在执行'bundle'命令进行更新/安装后,当我尝试执行rails srails g mongoid:config时,控制台返回以下以...开头的消息:

/home/myUser/proyect/config/environments/development.rb:50:in `block in <top (required)>': uninitialized constant ActiveSupport::EventedFileUpdateChecker (NameError)

这是我的Gemfile(是的,我想使用MongoDB作为数据库):
source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.6'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Mongoid as the database
gem 'mongoid', '~> 5.1.0'
# Use bson
gem 'bson_ext'
# Use Puma as the app server
gem 'puma', '~> 3.0'
#Use Haml for html
gem 'haml'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5.x'
# 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'

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

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console'
  gem 'listen', '~> 3.0.5'
  # 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]

还有config/environments/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.cache_store = :memory_store
    config.public_file_server.headers = {
      'Cache-Control' => 'public, max-age=172800'
    }
  else
    config.action_controller.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

  config.action_mailer.perform_caching = 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

  # 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

你能编辑你的development.rb文件吗? - Ronan Lopes
当然,刚刚添加了,现在可以观看。 - Marcos R. Guevara
3个回答

5
根据 Rails changelogsRails 5 引入了 config.file_watcher 选项。它允许您根据文件更改自动重新加载。此功能依赖于 listener gem,在您的gemfile中列出。但是我怀疑的是您的 Rails 版本!请确认您的版本是否支持此功能。
gem 'rails', '4.2.6'
# but rails 4 does not support that feature!

看起来你复制了另一个版本的Rails框架的Gemfile(或development.rb配置文件),另一个项目,或手动更改了gemfile版本以不适当的状态。

我可以向你建议两个选项:

  1. To change your rails gemfile version to the cutting edge one as follows:

    gem 'rails', github: 'rails/rails'
    

    And bundle once more;

  2. To remove config.file_watcher line from your config file.


1
首先我安装了最新版本,后来改成了'4.2.6'。 - Marcos R. Guevara

2

简而言之:重新创建Rails项目,并提供具体的Rails版本号。

通常这种情况是由于你安装了比.railsrctemplate.rb中要求的版本更高的Rails版本。

如果是这种情况,在运行rails new my_new_app命令时,最新版本将默认用于前面的步骤,但一旦安装了模板/railsrc版本,后续步骤将使用这个版本,这会导致兼容性问题。

你可以通过比较在rails new命令所在目录下运行rails -v输出和.railsrctemplate.rb中的内容来验证是否存在此问题。如果不同,有一个简单的解决方法:

重新创建你的Rails应用程序,并在命令行调用中指定与.railsrctemplate.rb中相同的Rails版本号:
rails _4.2.5.1_ new my_new_app


重新创建你的Rails应用似乎有些极端。在开发了约160个小时后,我正在添加一个依赖于较旧版本Rails的扩展。 - Wylliam Judd
是的,这是假设您早期发现了问题。正如OP所述,尝试执行rails grails s之类的操作将导致此错误。因此,在大多数情况下,它应该在其他事情做得太多之前发生。 - Scott Schupbach

1
如果您删除 config.file_watcher = ActiveSupport::EventedFileUpdateChecker,它将起作用。 它检测源代码的更改以异步刷新,但它还依赖于listen gem,由于您是初学者,它只会给您带来麻烦。 没有它也可以工作。

当我移除 config.file_watcher = ActiveSupport::EventedFileUpdateChecker 这行代码时,发生了另一个错误/callback_terminator.rb:6:in <top (required)>': undefined method halt_callback_chains_on_return_false=' for ActiveSupport:Module (NoMethodError) - Marcos R. Guevara

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