【问题标题】:All rails commands returning identical error所有 rails 命令都返回相同的错误
【发布时间】:2021-09-20 22:18:01
【问题描述】:

我遇到了一个奇怪的错误,没有任何类型的 rails 命令正在执行。每当我输入 rails 命令时,我都会得到以下信息:

忽略 racc-1.5.2,因为它的扩展未构建。试试: gem pristine racc --version 1.5.2

(在下面重复大约 100 次):

bin/rails:2:in `load': /Users/robertmorris/Desktop/All Projects/Code School/my9s/bin/spring:11: syntax error, unexpected keyword_rescue, expecting keyword_end (SyntaxError) rescue Gem::LoadError ^ /Users/robertmorris/Desktop/All Projects/Code School/my9s/bin/spring:14: syntax error, unexpected keyword_end, expecting end-of-input from bin/rails:2:in `<main>'

我的 bin/spring 文件:

if !defined?(Spring) && [nil, "development", "test"].include?(ENV["RAILS_ENV"])
  gem "bundler"
  require "bundler"

  # Load Spring without loading other gems in the Gemfile, for speed.
  Bundler.locked_gems&.specs&.find { |spec| spec.name == "spring" }&.tap do |spring|
    Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
    gem "spring", spring.version
    require "spring/binstub"
  rescue Gem::LoadError
    # Ignore when Spring is not installed.
  end
end

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    您使用的是旧的(不受支持的)ruby 版本。

    您应该使用任何可用的本地版本管理器(例如 rvmrbenvasdf)将其升级到至少 2.5,或者更好的 2.7 或 3。


    在 ruby​​ 2.4 和之前的版本中,您只能在任一方法中进行救援:

    def foo
      # do something
    rescue
      # do something else
    end
    

    或明确的begin/end 块:

    begin
      # do something
    rescue
      # do something else
    end
    

    自从 ruby​​ 2.5 发布以来,所有do/end 块都可以有rescue 子句:

    foo.bar.baz.tap do
      # do something
    rescue
      # do something else
    end
    

    在此之前你必须这样做

    foo.bar.baz.tap do
      begin
        # do something
      rescue
        # do something else
      end
    end
    

    【讨论】:

    • 所以答案是将 Ruby 更新到至少 2.5。几天前 Rails 7.0 alpha 发布了,它至少需要 Ruby 2.7。因此,我建议更新到 Ruby 2.7 甚至更好的 3.0。
    • 感谢@spickermann 的评论,我会让答案更清楚
    猜你喜欢
    • 2018-06-06
    • 2011-06-06
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-13
    • 1970-01-01
    • 2021-11-11
    相关资源
    最近更新 更多