在Windows上启动Rails v4.1.0服务器时出现TZInfo :: DataSourceNotFound错误

79

我使用 Ruby on Rails v4.1.0 创建了一个新的应用程序。在Windows上尝试启动服务器或控制台时,遇到以下错误:

$ rails server
Booting WEBrick
Rails 4.1.0 application starting in development on ....

Exiting
c:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/tzinfo-1.1.0/lib/tzinfo/data_source.rb:199:
in `rescue in create_default_data_source': 
No timezone data source could be found. To resolve this, either install 
TZInfo::Data (e.g. by running `gem install tzinfo-data`) or specify a zoneinfo 
directory using `TZInfo::DataSource.set(:zoneinfo, zoneinfo_path)`.
(TZInfo::DataSourceNotFound) 
from c:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/tzinfo-1.1.0/lib/tzinfo/data_source.rb:196:
in `create_default_data_source'

我该如何解决这个错误?


你按照错误提示做了吗?例如:“gem install tzinfo-data”或使用“TZInfo :: DataSource.set(:zoneinfo,zoneinfo_path)”指定一个zoneinfo目录。如果已经这样做了,结果如何? - Pavling
14个回答

176

解决错误

要解决这个错误,您需要确保在您的Gemfile中包含了tzinfo-data宝石。

首先,请检查您的Gemfile是否已有对tzinfo-data的引用。如果没有,请添加以下行:

gem 'tzinfo-data'

您可能会发现已经有一行类似于以下内容的行:

gem 'tzinfo-data', platforms: [:mingw, :mswin]
如果你正在Windows上使用64位版本的Ruby,则应将:x64_mingw添加到平台列表中,如下所示:
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]

或者,您可以完全删除platforms选项。

请注意,Bundler存在一个错误,这意味着目前没有平台选项可以匹配Windows上64位Ruby 3.1。解决方案是删除platforms选项。

完成此操作后,请在命令行中运行bundle update以安装tzinfo-data gem,然后您将能够启动Rails服务器或控制台。


背景

TZInfo是Rails Active Support组件的依赖项之一,会引发TZInfo::DataSourceNotFound错误。TZInfo正在查找系统中的时区数据源,但未能找到。

在许多基于Unix的系统(例如Linux)上,TZInfo能够使用系统的zoneinfo目录作为数据源。但是,Windows没有这样的目录,因此需要安装tzinfo-data gem。tzinfo-data gem包含相同的zoneinfo数据,打包成一组Ruby模块。

Rails在创建应用程序时会生成默认的Gemfile。如果在Windows上创建应用程序,则会包括对tzinfo-data的依赖项。但是,Rails(至版本4.1.x)从平台列表中省略了:x64_mingw,因此在Ruby的64位Windows版本上不能正常工作。


1
@MarkBoulder 目前在 OpenBSD 上需要 tzinfo-data,因为当前版本的 TZInfo 无法使用 OpenBSD 的系统 zoneinfo 目录。在 git 存储库中有一个修复方案。这将包含在下一个 TZInfo 发布中。 - Phil Ross
从 https://github.com/tzinfo/tzinfo/wiki/Resolving-TZInfo::DataSourceNotFound-Errors解决 TZInfo::DataSourceNotFound 错误 - Matthieu
1
@Blankman,你可以运行bundle platform来确定当前平台。 - Phil Ross
1
这些解决方案都不适用于我现代的环境,所以我只能将 gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]替换为gem 'tzinfo-data', '~> 1.2021', '>= 1.2021.5' - DrQuarius
3
@DrQuarius 如果你在Windows上使用Ruby 3.1,那么你可能会遇到Bundler中的一个bug:https://github.com/rubygems/rubygems/issues/5269。没有平台选项可以匹配Windows上的64位Ruby 3.1。解决方案是删除平台选项(版本约束不需要)。 - Phil Ross
显示剩余7条评论

16

我必须添加两个 gem 才能启动服务器...

gem 'tzinfo-data'
gem 'tzinfo'

然后执行 bundle install 命令。


5
这些解决方案都不适用于我现代化的环境,相反我不得不将 gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] 替换为 gem 'tzinfo-data', '~> 1.2021', '>= 1.2021.5' - DrQuarius

15

我在尝试在Docker容器中安装Redmine时遇到了这个错误:

RAILS_ENV=production bundle exec rake db:migrate

因为我的 Ubuntu 镜像中没有安装 tzdata 包,所以出现了这个错误。

apt-get update && apt-get install tzdata -y

事情成功了。


9

只需将此代码放入应用程序终端:

gem install tzinfo-data

然后将Gemfile行更改为:

gem 'tzinfo-data', platforms: [:x64_mingw, :mingw, :mswin]

然后再次在您的终端中输入:

bundle update

那样可以直接解决问题。

6
将以下行添加到您的Gem文件中:

gem 'tzinfo-data',platforms: [:x64_mingw,:mingw, :mswin]

这个Gem可以帮助您在不同的操作系统平台上使用时区信息。

4
Gemfile中,我添加了以下代码行
#tzinfo-data
gem 'tzinfo-data', '~> 1.2021', '>= 1.2021.5'

然后,我将以下行注释掉了

gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]

然后我简单地运行了

rails server

并且它对我来说成功了。

这是我的完整的 Gemfile

source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.1.1"

#tzinfo-data
gem 'tzinfo-data', '~> 1.2021', '>= 1.2021.5'

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.2", ">= 7.0.2.3"

# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"

# Use sqlite3 as the database for Active Record
gem "sqlite3", "~> 1.4"

# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"

# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"

# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"

# Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0"

# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"

# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
# gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false

# Use Sass to process CSS
# gem "sassc-rails"

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

group :development, :test do
  # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
  gem "debug", platforms: %i[ mri mingw x64_mingw ]
end

group :development do
  # Use console on exceptions pages [https://github.com/rails/web-console]
  gem "web-console"

  # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
  # gem "rack-mini-profiler"

  # Speed up commands on slow machines / big apps [https://github.com/rails/spring]
  # gem "spring"
end

group :test do
  # Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
  gem "capybara"
  gem "selenium-webdriver"
  gem "webdrivers"
end

3
也许您的系统上没有安装tzinfo,请尝试安装它:
 gem install tzinfo
 gem install tzinfo-data

您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - TSV

1

我也遇到了这个问题,通过将:x64_mingw添加到tzinfo-data的平台列表中,并在gemfile中添加gem 'tzinfo'来解决它。然后运行bundle install。


1
我在macOS Mojave 10.14.5上遇到了这个问题,发现是因为我的符号链接在macOS上没有读取正确的区域信息文件。我使用命令TZInfo::ZoneinfoDataSource.search_path来跟踪文件应该在哪里,输出结果为["/usr/share/zoneinfo", "/usr/share/lib/zoneinfo", "/etc/zoneinfo"]。
我开始查看/usr/share/zoneinfo并找到可读的文件。然而,Rails仍然无法找到它们,读取它们,执行它们...?因此,我创建了一个从/usr/share/zoneinfo.default中的另一个文件到/etc/zoneinfo(TZInfo查找的最后一个路径)的符号链接。
所以最终解决这个问题的命令是ln -s /usr/share/zoneinfo.default /etc/zoneinfo。
希望这些信息对未来的某个人有所帮助。

0

我正在使用Windows,几天来一直无法解决这个问题。 因此,我们64位平台的解决方案是在终端中键入以下内容:

gem install tzinfo
gem install tzinfo-data

之后,请导航回到您的gem文件。如果您还没有创建,请在终端上使用rails new xyznameofyours创建它。

然后将声明类似于以下代码行的代码行替换为: gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw] gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] 改成:

gem 'tzinfo-data'
gem 'tzinfo'

只有在你的代码能够正常工作之后!


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