我是Ruby on Rails的新手。运行"bundle"命令进行更新/安装后,当我尝试执行rails s
或rails 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/environment/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
根据 Rails 更新日志,config.file_watcher
选项已在Rails 5
中引入。它允许您根据文件更改自动重新加载。此功能取决于 Gemlistener
,并且您在 gem 文件中列出了它。但在我看来可疑的是你的Rails
版本!
gem 'rails', '4.2.6'
# but rails 4 does not support that feature!
看起来您已经从另一个Rails
版本的框架、另一个项目复制了一个 Gemfile(或development.rb
配置?),或者手动将您的 gemfile 版本更改为不适当的状态。
我可以建议你的两个选择是:
将您的 rails gemfile 版本更改为最前沿的版本,如下所示:
gem 'rails', github: 'rails/rails'
再
bundle
一次;从配置文件中删除
config.file_watcher
行。
tl;dr:重新创建你的 Rails 项目,提供特定的 Rails 版本。
通常发生这种情况是因为您安装了比.railsrc
或template.rb
中要求的更新版本的 Rails。
如果是这种情况,当您运行rails new my_new_app
时,默认情况下将最新版本用于该过程的前面步骤,但是一旦安装了 template/railsrc 版本,后面的步骤将使用此版本。这会导致兼容性问题。
您可以通过将rails -v
的输出(在您调用rails new
的目录中)与.railsrc
或template.rb
中的输出进行比较来验证这是否是您的问题。如果它们不同,则有一个简单的解决方法:
重新创建您的 Rails 应用程序,在命令行调用中从您的.railsrc
或template.rb
指定相同的 Rails 版本:rails _4.2.5.1_ new my_new_app
如果你删除config.file_watcher = ActiveSupport::EventedFileUpdateChecker
它将起作用。它会检测源代码中的更改以异步刷新,但它也取决于 gemlisten
,并且由于您是初学者,它只会给您带来麻烦。应该在没有它的情况下工作。