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

Joh*_*ohn 63 ruby ruby-on-rails tzinfo ruby-on-rails-4.1

我使用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'
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个错误?

Phi*_*oss 151

解决错误

要解决此错误,您需要确保tzinfo-data gem包含在您的Gemfile.

首先,检查您Gemfile是否存在现有的引用tzinfo-data.如果还没有引用,则添加以下行:

gem 'tzinfo-data'
Run Code Online (Sandbox Code Playgroud)

您可能会发现已有如下所示的行:

gem 'tzinfo-data', platforms: [:mingw, :mswin]
Run Code Online (Sandbox Code Playgroud)

如果您在Windows上使用64位版本的Ruby,则添加:x64_mingw到平台列表,如下所示:

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]
Run Code Online (Sandbox Code Playgroud)

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

执行此操作后,bundle update在命令行运行以安装tzinfo-data gem,然后您就可以启动Rails服务器或控制台.


背景

TZInfo::DataSourceNotFound误差是由TZInfo,滑道的主动支持组件的依赖性提高.TZInfo正在寻找您系统上的时区数据源,但未能找到.

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

Gemfile首次创建应用程序时,Rails会生成默认值.如果在Windows上创建应用程序,则将包含tzinfo-data的依赖项.但是(从Rails版本4.1.0开始),这:x64_mingw从平台列表中省略,因此在64位Windows版本的Ruby上无法正常工作.这应该在将来的Rails版本中修复.

  • @DrQuarius 如果您在 Windows 上使用 Ruby 3.1,那么您可能会在 Bundler 中遇到错误:https://github.com/rubygems/rubygems/issues/5269。没有任何平台选项可以与 Windows 上的 64 位 Ruby 3.1 相匹配。解决方案是删除平台选项(不需要版本约束)。 (3认同)

Rus*_*voe 14

我不得不添加两个宝石来启动服务器..

宝石'tzinfo-
data'gem'tzinfo'

然后捆绑安装.

  • 这些解决方案在现代都不适合我,相反,我必须将 `gem "tzinfo-data",platforms: %i[ mingw mswin x64_mingw jruby ]` 替换为 `gem 'tzinfo-data', '~> 1.2021', '>= 1.2021.5'` (5认同)

Adl*_*dly 8

把它放在你的app终端:

gem install tzinfo-data
Run Code Online (Sandbox Code Playgroud)

然后将gemfile行更改为:

gem 'tzinfo-data', platforms: [:x64_mingw, :mingw, :mswin]
Run Code Online (Sandbox Code Playgroud)

然后再次在你的终端:

bundle update
Run Code Online (Sandbox Code Playgroud)

这将直接解决问题.


HaT*_*SuM 5

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

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


Mat*_*ieu 5

尝试在Docker容器中安装Redmine时遇到该错误:

RAILS_ENV=production bundle exec rake db:migrate
Run Code Online (Sandbox Code Playgroud)

给我该错误,因为tzdata未在我的Ubuntu映像中安装软件包。

apt-get update && apt-get install tzdata -y
Run Code Online (Sandbox Code Playgroud)

做到了。