truongnm
truongnm

Reputation: 2474

Command: rails console, generate not recognize. Error: Command 'rails' not recognized Usage: rails COMMAND [ARGS]

When I go to rails folder (I'm inside my rails app folder) and type "rails c" or "rails g" it said:

Error: Command 'rails' not recognized

Usage: 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.

I'm running Linux Subsystem for Windows 10, I know I know you see Windows but it's kind of... Linux, and I installed Ruby by rbenv through this tutorial.

Upvotes: 3

Views: 3064

Answers (2)

Robin
Robin

Reputation: 915

Even though the accepted answer solved your issue, I want to add another because I ran into the same error message but the underlying issue and my environment setup wasn't the same. However, recreating the executables in my app's bin folder did not solve the problem.

The issue: When I tried running bundle exec rails s or bundle exec rails c I got the same error as posted in the question. Using the apps executables worked fine though ./bin/rails s.

So the main differences from the question: I was using bundle exec when I got the error.

What was happening behind the scenes: bundle exec rails s was calling this executable /Users/robin/.rvm/gems/ruby-2.3.8/bin/rails which is shipped by the railties gem. I found this file was overwritten due to a gem which had an executable rails defined in its .gemspec.

What solved my problem:

Running $ bundle pristine railties (https://bundler.io/v2.0/man/bundle-pristine.1.html) so railties would be reset and with that the executable shipped by railties would overwrite the wrong one.

Addition: Of course it only helps as long as the intrusive gem shipping a rails executable is also updated and no longer wants it to be installed.

Upvotes: 1

J Fournier
J Fournier

Reputation: 300

Try this command from your app folder:

rake rails:update:bin

Say y to replace the binaries.

I had the same problem using rails with Windows Linux Subsystem. I found the suggestion here:

Rails command Error

I tried it and it solved my problem, so maybe yours as well.

Upvotes: 6

Related Questions