Ruby on rails的“bin/rails路径”和“bin/rails服务器”无法工作

axr492tv  于 2022-10-15  发布在  Ruby
关注(0)|答案(2)|浏览(197)

最近开始学习Ruby on rails来构建一个Web API,用VS代码编写代码,跟随YouTube视频:https://www.youtube.com/watch?v=6KqbPJtA5O8&list=PLbTv9eGiI03u1-JFkFpPGsR_hMre6WX3e
安装完所有内容后,使用rails new apiproject --api创建一个项目
下一步是使用bin/rails routes检查路由,得到错误

Error: Command 'routes' not recognized
Did you mean: `$ rake routes` ?

Usage: rails COMMAND [ARGS]

The most common rails commands are:
 generate    Generate new code (short-cut alias: "g")
 console     Start the Rails console (short-cut alias: "c")
 server      Start the Rails server (short-cut alias: "s")
 dbconsole   Start a console for the database specified in config/database.yml
             (short-cut alias: "db")
 new         Create a new Rails application. "rails new my_app" creates a
             new application called MyApp in "./my_app"

In addition to those, there are:
 destroy      Undo code generated with "generate" (short-cut alias: "d")
 plugin new   Generates skeleton for developing a Rails plugin
 runner       Run a piece of code in the application environment (short-cut alias: "r")

All commands can be run with -h (or --help) for more information.

在视频中,它应该返回

我试过rake route,但结果并不像我在视频中预期的那样。
在那之后,我尝试先使用bin/rails server启动服务器,但一直收到错误:

/Users/jolin/.rvm/gems/ruby-2.7.0/gems/activesupport-4.2.2/lib/active_support/core_ext/object/duplicable.rb:85: warning: BigDecimal.new is deprecated; use BigDecimal() method instead.
=> Booting WEBrick
=> Rails 4.2.2 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
/Users/jolin/.rvm/gems/ruby-2.7.0/gems/activesupport-4.2.2/lib/active_support/core_ext/numeric/conversions.rb:121: warning: constant ::Fixnum is deprecated
/Users/jolin/.rvm/gems/ruby-2.7.0/gems/activesupport-4.2.2/lib/active_support/core_ext/numeric/conversions.rb:121: warning: constant ::Bignum is deprecated
Exiting
Traceback (most recent call last):
        9377: from bin/rails:3:in `<main>'
        9376: from bin/rails:3:in `load'
        9375: from /Users/jolin/Desktop/RoR/liborapi/bin/spring:15:in `<top (required)>'
        9374: from /Users/jolin/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:in `require'
        9373: from /Users/jolin/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:in `require'
        9372: from /Users/jolin/.rvm/gems/ruby-2.7.0/gems/spring-4.1.0/lib/spring/binstub.rb:11:in `<top (required)>'
        9371: from /Users/jolin/.rvm/gems/ruby-2.7.0/gems/spring-4.1.0/lib/spring/binstub.rb:11:in `load'
        9370: from /Users/jolin/.rvm/gems/ruby-2.7.0/gems/spring-4.1.0/bin/spring:49:in `<top (required)>'
         ... 9365 levels...
           4: from /Users/jolin/.rvm/gems/ruby-2.7.0/gems/activesupport-4.2.2/lib/active_support/core_ext/numeric/conversions.rb:131:in `block (2 levels) in <class:Numeric>'
           3: from /Users/jolin/.rvm/gems/ruby-2.7.0/gems/activesupport-4.2.2/lib/active_support/core_ext/numeric/conversions.rb:131:in `block (2 levels) in <class:Numeric>'
           2: from /Users/jolin/.rvm/gems/ruby-2.7.0/gems/activesupport-4.2.2/lib/active_support/core_ext/numeric/conversions.rb:131:in `block (2 levels) in <class:Numeric>'
           1: from /Users/jolin/.rvm/gems/ruby-2.7.0/gems/activesupport-4.2.2/lib/active_support/core_ext/numeric/conversions.rb:131:in `block (2 levels) in <class:Numeric>'
/Users/jolin/.rvm/gems/ruby-2.7.0/gems/activesupport-4.2.2/lib/active_support/core_ext/numeric/conversions.rb:131:in `block (2 levels) in <class:Numeric>': stack level too deep (SystemStackError)

我之前收到了undefined method <class:BigDecimal>错误,但在将gem 'bigdecimal', '1.4.2'添加到Gemfile后,它得到了解决,但我找不到解决这个问题的方法。
我甚至在第一步就一直在挣扎,我试图升级/降级我的Ruby和Rails的不同版本……
我当前的版本:

RubyRuby 2.7.0p0(2019-12-25修订版647ee6f091)[x86_64-darwin19]
railsrails 4.2.2

(但使用rails-v时,我总是收到弃用警告:‘--rc’的字符串缺省值;得到FALSE(布尔值)。
这将在将来被拒绝,除非您在代码中显式传递选项check_default_type: false或调用allow_incompatible_default_type!。您可以通过设置环境变量TORSIME_DEPERATION来使弃用警告静音。)

rvmrvm 1.29.12(最新),作者:Michal Papis,Piotr Kuczynski,Wayne E.Sguin[https://rvm.io]GEM3.1.2
更新在尝试升级ruby、降级ruby、降级rails后,现在运行bin/rails server时不断收到如下错误:

Traceback (most recent call last):
        4: from bin/rails:3:in `<main>'
        3: from bin/rails:3:in `load'
        2: from /Users/jolin/Desktop/RoR/liborapi/bin/spring:10:in `<top (required)>'
        1: from /Users/jolin/Desktop/RoR/liborapi/bin/spring:10:in `read'
/Users/jolin/Desktop/RoR/liborapi/bin/spring:10:in `read': No such file or directory @ rb_sysopen - /Users/jolin/Desktop/RoR/liborapi/Gemfile.lock (Errno::ENOENT)
aij0ehis

aij0ehis1#

我建议在Ruby 2.5.3中运行它,如屏幕截图所示。我认为Ruby 2.7中没有测试或支持rails 4.2。您还可以尝试安装更高版本的Ruby 2.7.6。
Rails的当前版本是7.0.x,而Ruby是3.1.x。
分段错误通常是Ruby安装的结果,您可以尝试安装Ruby 2.7.6,然后重试。

t2a7ltrp

t2a7ltrp2#

在Ruby on rails 5.0中引入了rails routes命令。在5.0之前的版本中,它被称为rake routes
这意味着,由于您使用的是旧版本的Rails,因此只需调用rake routes即可获得相同的响应。
但值得注意的是,您正在关注的YouTube视频是关于Ruby on rails 6.0的。但是您使用的是Ruby on rails 4.2.2。您的本地环境是两个主要版本,并且落后了4年多。当您继续使用这样一个古老且未经维护的Ruby on rails版本时,您很可能会面临更多的兼容性问题。
我建议您至少将本地环境升级到Ruby 2.7和Ruby on rails 6.2,甚至更好的Ruby 3.1和Ruby on rails 7.0。了解Rails是过时的和未维护的版本是没有好处的。

相关问题