【问题标题】:TZInfo::DataSourceNotFound error starting Rails v4.1.0 server on WindowsTZInfo::DataSourceNotFound 在 Windows 上启动 Rails v4.1.0 服务器时出错
【发布时间】:2014-04-11 20:45:40
【问题描述】:

我使用 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 目录。如果有,结果如何?

标签: ruby-on-rails ruby ruby-on-rails-4.1 tzinfo


【解决方案1】:

解决错误

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

首先,检查您的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 选项。

请注意,bug in Bundler 表示当前没有与 Windows 上的 64 位 Ruby 3.1 匹配的平台选项。这里的解决方案是去掉platforms选项。

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


背景

TZInfo::DataSourceNotFound 错误是由 Rails 的 Active Support 组件的依赖项 TZInfo 引发的。 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,因此在 64 位 Windows 版本的 Ruby 上无法正常工作。

【讨论】:

  • tzinfo-data 是否必须永久存在于 Gemfile 中,还是仅在 Rails 团队修复某些问题之前?
  • @MarkBoulder 如果您想在 Windows 上运行 Rails 项目,您需要将 tzinfo-data 永久保存在 Gemfile 中。
  • 我正在使用 OpenBSD,看来我也需要它。希望它已经在新生成的 Rails 应用程序的 Gemfile 中?
  • @MarkBoulder tzinfo-data 目前在 OpenBSD 上是必需的,因为当前版本的 TZInfo is not able to use OpenBSD's system zoneinfo directory。 git 存储库中有一个fix。这将包含在下一个 TZInfo 版本中。
  • @Blankman 你可以运行bundle platform 来确定当前平台。
【解决方案2】:

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

gem 'tzinfo-data'
宝石'tzinfo'

然后捆绑安装。

【讨论】:

  • 这些解决方案在现代都不适合我,我不得不用gem 'tzinfo-data', '~> 1.2021', '>= 1.2021.5'替换gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
【解决方案3】:

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

RAILS_ENV=production bundle exec rake db:migrate

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

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

成功了。

【讨论】:

    【解决方案4】:

    只需将其放入您的应用程序终端即可:

    gem install tzinfo-data
    

    然后将 gemfile 行更改为:

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

    然后在您的终端中再次:

    bundle update
    

    这样就直接解决问题了。

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

        可能你的系统没有安装tzinfo,试试安装:

         gem install tzinfo
         gem install tzinfo-data
        

        【讨论】:

        • 还将“gem 'tzinfo-data',platforms: [:mingw, :mswin, :x64_mingw]” 添加到“\Ruby21-x64\lib\ruby\gems\2.1.0\gems\ railties-4.1.6\lib\rails\generators\rails\app\templates\Gemfile”文件。然后这将默认添加到所有新应用中。
        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

          我在 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

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

          【讨论】:

            【解决方案9】:

            我使用的是 Windows,但几天来我都无法解决这个问题。 所以我们 64 位平台的解决方案是在终端输入以下内容:

            gem install tzinfo
            gem install tzinfo-data
            

            然后返回您的 gem 文件。如果您还没有创建一个,请在终端上使用 rails new xyznameofyours 创建它。

            然后替换如下代码行: gem 'tzinfo-data',平台:[:mingw, :mswin, :x64_mingw] gem "tzinfo-data",平台:%i[ mingw mswin x64_mingw jruby ] 对此:

            gem 'tzinfo-data'
            gem 'tzinfo'
            

            只有这样你的代码才能工作!

            【讨论】:

              【解决方案10】:

              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
              

              【讨论】:

                【解决方案11】:

                直接通过 cmd 安装 gem 可能无法解决 Windows 操作系统上的问题。 以下是它对我的逐步工作方式;

                1. 在 Gemfile 文件夹中“ctrl-F”并输入“platforms”

                2. 找到行 gem "tzinfo-data", 平台: %i[ mingw mswin x64_mingw jruby ]

                3. 删除“tzinfo-data”之后的所有内容(包括逗号)

                4. 最终视图应该只是 gem "tzinfo-data" 还添加 gem "tzinfo"

                5. 在终端输入“bundle update”或“bundle update”

                6. 完成后,输入“rails s”

                注意:即使在此之后,如果您的服务器启动但无法解析默认页面(另外 ctrl-c / ctrl-pause/break em> 不起作用),然后通过添加 gem gem 'webrick', '~> 1.3', '>= 1.3.1' 来添加 webrick 而不是 puma。之后,要运行 webrick 服务器,您需要输入 rails s -u webrick

                【讨论】:

                  【解决方案12】:

                  所以,gem 似乎没有正确安装,我必须执行以下操作

                  gem 'tzinfo 数据' 宝石'tzinfo'

                  然后

                  捆绑展示 看到所有的宝石

                  捆绑 gem tzinfo 将为您提供 gem 的目录

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

                  那么 去 local_pathname/tzinfo-1.2.1/lib 并打开文件tzinfo(不是目录) 并添加这一行 需要'tzinfo/data'

                  想起来真是太痛苦了

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2012-06-29
                    • 1970-01-01
                    • 2012-04-18
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多