命令:rails console,generate不被识别。错误:未识别命令“rails”。用法:rails 命令 [参数]。

3
当我进入Rails文件夹(我在我的Rails应用程序文件夹内部)并输入"rails c"或"rails g"时,它会显示以下内容:
错误:未识别命令'rails'
用法:rails COMMAND [ARGS]
Usage: spring COMMAND [ARGS]                                                                                                                     

Commands for spring itself:                                                                                                                      

  binstub         Generate spring based binstubs. Use --all to generate a binstub for all known commands. Use --remove to revert.                
  help            Print available commands.                                                                                                      
  server          Explicitly start a Spring server in the foreground                                                                             
  status          Show current status.                                                                                                           
  stop            Stop all spring processes for this project.                                                                                    

Commands for your application:                                                                                                                   

  rails           Run a rails command. The following sub commands will use spring: console, runner, generate, destroy, test.                     
  rake            Runs the rake command                                                                                                          
Error: Command 'rails' not recognized                                                                                                            
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.

我正在运行适用于Windows 10的Linux子系统,我知道你看到了Windows,但它有点像... Linux,并且我通过这个教程使用rbenv安装了Ruby。


好的,我会尝试一下。但是我还能通过“rails new app”创建项目吗? - truongnm
1
这可能会对你有所帮助 http://stackoverflow.com/questions/21608744/rails-command-error - Tony Vincent
2个回答

6

在您的应用程序文件夹中尝试这个命令:

rake rails:update:bin

输入 y 以替换二进制文件。

我在使用 Windows Linux 子系统的 rails 时遇到了同样的问题。我在这里找到了建议:

Rails command Error

我尝试了它,解决了我的问题,也许可以解决你的问题。


1
即使已经有了被接受的答案解决了你的问题,我还想再补充一些内容,因为我遇到了相同的错误信息,但是根本原因和我的环境设置不同。然而,在我的应用程序的bin文件夹中重新创建可执行文件并不能解决这个问题。
问题在于:当我尝试运行bundle exec rails sbundle exec rails c时,我得到了与问题中发布的相同的错误。然而,使用应用程序的可执行文件却很好用,比如./bin/rails s
所以与问题的主要区别在于:当我遇到这个错误时,我正在使用bundle exec
背后发生了什么: bundle exec rails s调用了这个可执行文件/Users/robin/.rvm/gems/ruby-2.3.8/bin/rails,它由railties gem提供。我发现由于一个gem定义了一个可执行文件rails在它的.gemspec中,这个文件被覆盖了。
解决我的问题的方法:
运行$ bundle pristine railtieshttps://bundler.io/v2.0/man/bundle-pristine.1.html),这样railties将被重置,并且由railties提供的可执行文件将覆盖错误的文件。
补充说明:当然,只要具有侵入性的gem更新并不再希望安装它,它就会有所帮助。

它运行得很好:“bundle exec bin/rails s”只需提供Rails应用程序文件夹中可执行文件的路径。 - Aivils Štoss

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接