Ruby-on-rails TZInfo::DataSourceNotFound 在 Windows 上启动 Rails v4.1.0 服务器时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23022258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:32:39  来源:igfitidea点击:

TZInfo::DataSourceNotFound error starting Rails v4.1.0 server on Windows

ruby-on-railsrubyruby-on-rails-4.1tzinfo

提问by John

I have created a new application using Ruby on Rails v4.1.0. When attempting to start a server or console on Windows, I am encountering the following error:

我使用 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'

How can I resolve this error?

我该如何解决这个错误?

回答by Phil Ross

Resolving the Error

解决错误

To resolve this error, you'll need to make sure that the tzinfo-data gem is being included in your Gemfile.

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

First of all, check your Gemfileto see if there is an existing reference to tzinfo-data. If there isn't already a reference, then add the following line:

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

gem 'tzinfo-data'

You may find that there is already a line like the following:

你可能会发现已经有如下一行:

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

If you are using a 64-bit version of Ruby on Windows, then add :x64_mingwto the list of platforms as follows:

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

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

Alternatively, you can remove the platformsoption altogether.

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

After doing this, run bundle updateat the command line to install the tzinfo-data gem and you'll then be able to start your Rails server or console.

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


Background


背景

The TZInfo::DataSourceNotFounderror is being raised by TZInfo, a dependency of the Active Support component of Rails. TZInfo is looking for a source of time zone data on your system, but failing to find one.

TZInfo::DataSourceNotFound错误是由 Rails 的 Active Support 组件的依赖项 TZInfo 引发的。TZInfo 正在您的系统上寻找时区数据的来源,但未能找到。

On many Unix-based systems (e.g. Linux), TZInfo is able to use the system zoneinfodirectory as a source of data. However, Windows doesn't include such a directory, so the tzinfo-data gem needs to be installed instead. The tzinfo-data gem contains the same zoneinfo data, packaged as a set of Ruby modules.

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

Rails generates a default Gemfilewhen the application is first created. If the application is created on Windows, then a dependency for tzinfo-data will be included. However (as of Rails version 4.1.0), this omits :x64_mingwfrom the list of platforms, so doesn't work correctly on 64-bit Windows versions of Ruby. This should be fixedin future Rails releases.

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

回答by Russell Clarvoe

I had to add two gems to get the server to start..

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

gem 'tzinfo-data'
gem 'tzinfo'

宝石“tzinfo-数据”
宝石“tzinfo”

Then bundle install.

然后捆绑安装。

回答by Adly

Just put this in your app terminal :

只需将其放在您的应用程序终端中:

gem install tzinfo-data

then change the gemfile line to :

然后将 gemfile 行更改为:

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

then again in your terminal :

然后再次在您的终端中:

bundle update

That will solve the problem directly.

这样可以直接解决问题。

回答by Matthieu

I had that error when trying to install Redmine in a Docker container:

尝试在 Docker 容器中安装 Redmine 时出现该错误:

RAILS_ENV=production bundle exec rake db:migrate

gave me the error because package tzdatawas not installed in my Ubuntu image.

给了我错误,因为tzdata我的 Ubuntu 映像中没有安装软件包。

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

did the trick.

成功了。

回答by HaTiMSuM

Add the following line to your Gem File

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

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

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

回答by M-Razavi

Maybe tzinfo is not installed on your system, try to install it:

也许你的系统上没有安装 tzinfo,尝试安装它:

 gem install tzinfo
 gem install tzinfo-data

回答by Stu Horsfield

I had this problem too and fixed it by adding BOTH the :x64_mingwto the list of platforms for tzinfo-data, AND the gem 'tzinfo' to the gemfile. Then bundle install.

我也遇到了这个问题,并通过将 BOTH 添加:x64_mingw到平台列表中来修复它,并将tzinfo-datagem 'tzinfo' 添加到 gemfile 中。然后捆绑安装。

回答by Jonathan Fairbanks

I ran into this issue on macOs Mojave 10.14.5 and I found out that it was something with how my symlink in macOs wasn't reading the correct provided zone info files.

我在 macOs Mojave 10.14.5 上遇到了这个问题,我发现这与我在 macOs 中的符号链接没有读取正确提供的区域信息文件有关。

I was able to track this down with where the files should be using the command TZInfo::ZoneinfoDataSource.search_pathand that provided the output of ["/usr/share/zoneinfo", "/usr/share/lib/zoneinfo", "/etc/zoneinfo"].

我能够通过文件应该使用命令的位置来跟踪这一点, TZInfo::ZoneinfoDataSource.search_path并提供["/usr/share/zoneinfo", "/usr/share/lib/zoneinfo", "/etc/zoneinfo"].

I began looking into /usr/share/zoneinfoand there were files available to read. However rails still wasn't finding them, reading them, executing them..? So I then created a symlink from the other file in /usr/share/zoneinfo.default/to /etc/zoneinfo(the last path TZInfo looks up)

我开始调查/usr/share/zoneinfo,发现有文件可供阅读。然而,rails 仍然没有找到它们,读取它们,执行它们......?所以我然后从另一个文件中创建了一个符号链接/usr/share/zoneinfo.default//etc/zoneinfo(TZInfo查找的最后一个路径)

So finally the command that worked for me to fix this issue was ln -s /usr/share/zoneinfo.default /etc/zoneinfo

所以最后对我来说解决这个问题的命令是 ln -s /usr/share/zoneinfo.default /etc/zoneinfo

Hopefully this information is helpful to someone in the future.

希望这些信息对将来的某人有所帮助。

回答by slob

so, the gems didn't quite seem to install properly, i had to do the following

因此,gems 似乎没有正确安装,我必须执行以下操作

gem 'tzinfo-data' gem 'tzinfo'

宝石“tzinfo-数据”宝石“tzinfo”

then

然后

bundle show to see all gems

bundle gem tzinfo will get you the gem's directory

捆绑展示以查看所有宝石

bundle gem tzinfo 将为您提供 gem 目录

then, go to that directory. you will need to splice tzinfo-data into tzinfo. in the tzinfo-data file, go to.. local_pathname/tzinfo-data-1.2014.5/lib/tzinfo copy all the contents of this directory into... local_pathname/tzinfo-1.2.1/lib/tzinfo (for me this meant copying 'data' the file and 'data' the directory)

然后,转到该目录。您需要将 tzinfo-data 拼接到 tzinfo 中。在 tzinfo-data 文件中,转到.. local_pathname/tzinfo-data-1.2014.5/lib/tzinfo 将此目录的所有内容复制到... local_pathname/tzinfo-1.2.1/lib/tzinfo (对我来说这个意味着复制“数据”文件和“数据”目录)

then go to local_pathname/tzinfo-1.2.1/lib and open the file, tzinfo, (not the directory) and add this line require 'tzinfo/data'

然后转到 local_pathname/tzinfo-1.2.1/lib 并打开文件 tzinfo(不是目录)并添加这一行 require 'tzinfo/data'

this was such a pain to figure out

搞清楚这件事真是太痛苦了