问题
I'm a beginner with Rails and I've been trying to find out what in the world is going wrong here for the past few days but with absolutely no luck. I searched everywhere, but found nothing. Basically, when I run any sort of command line method, I will get a NameError. I have tried running this in different project directories as well that used to work. It seems that rails is just broken...
I am running Win7-32bit using NetBeans 6.9.1 for editing files and cmd.exe for running rails console.
Everything had been working fine for about two weeks and then this started happening suddenly. Here are a few examples:
rails generate model:
irb(main):004:0> rails generate model datatest
NameError: undefined local variable or method `datatest' for main:Object
from (irb):4
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
rails generate migration:
irb(main):003:0> rails generate migration rename_password_to_hashed_password
NameError: undefined local variable or method `rename_password_to_hashed_password' for main:Object
from (irb):3
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
rake db:migrate:
irb(main):005:0> rake db:migrate
NameError: undefined local variable or method `migrate' for main:Object
from (irb):5
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Any sort of help would be VERY appreciated. Thanks!
回答1:
These commands don't work from within irb, you have to start them directly from cmd.exe.
回答2:
I would take the initiative of elaborating @dhofset's answer. Basically we use rails app related commands in the console directly and not in the irb console. Even I work on windows so I give you this with few examples
This is what you do
1. Create new App
C:\Users\rohit>rails new_app
2. Generate a scaffold
C:\Users\rohit\new_app>ruby script\generate scaffold User name:string
3. Generate a model
C:\Users\rohit\new_app>ruby script\generate model User name:string
4. To run a rake task
C:\Users\rohit\new_app>rake db:create
C:\Users\rohit\new_app>rake db:migrate
Hope you have a better understanding now.
回答3:
Yep, dhofstet has it right: these are all commands for the windows prompt, they're not ruby statements.
来源:https://stackoverflow.com/questions/4395222/rails-gives-nameerror-for-all-command-line-methods-such-as-generate-or-rake