5

我有一个旧版 Rails 应用程序,我想升级到最新的 Rails 和 Ruby 版本。首先,我尝试使用 Ruby 2.1.2 设置应用程序

$ rails -v
Rails 2.3.18

$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [i686-linux]

当我尝试运行 rake 任务rake db:schema:load RAILS_ENV=test时,我遇到了以下错误

 can not load translations from /activesupport-2.3.18/lib/active_support/locale/en.yml, the file type yml is not known

通过 Google 搜索,我发现以下参考文献https://github.com/rails/rails/issues/10514提到 Rails 2.3 和 Ruby 2+ 版本之间不兼容。

有人可以帮我应用参考链接中提到的猴子补丁吗?

谢谢,吉涅什

4

1 回答 1

10

终于解决了错误

 can not load translations from /activesupport-2.3.18/lib/active_support/locale/en.yml, the file type yml is not known

通过猴子修补 Rails 的 I18n::Backend::Base#load_file(filename) 方法。

解决方法如下:

1.1 创建了一个名为ruby2.rb/config/initializers的文件

1.2 新增以下内容/config/initializers/ruby2.rb

  if Rails::VERSION::MAJOR == 2 && RUBY_VERSION >= '2.0.0'
    module I18n
      module Backend
        module Base
          def load_file(filename)
            type = File.extname(filename).tr('.', '').downcase
            # As a fix added second argument as true to respond_to? method
            raise UnknownFileType.new(type, filename) unless respond_to?(:"load_#{type}", true)
            data = send(:"load_#{type}", filename) # TODO raise a meaningful exception if this does not yield a Hash
            data.each { |locale, d| store_translations(locale, d) }
          end
        end
      end
    end
  end

1.3 终于跑了

   $ rake db:schema:load RAILS_ENV=test

并且模式已成功加载。

我能找到的最有用的参考资料,并帮助我找到了解决方案:

  1. https://github.com/rails/rails/issues/10514
  2. https://www.lucascaton.com.br/2014/02/28/have-a-rails-2-app-you-can-run-it-on-the-newest-ruby/
于 2014-07-30T12:18:44.713 回答