我正在阅读 Ruby on Rails 教程,当尝试在 Rails 控制台(沙盒模式)中保存用户时,出现以下错误:
NameError: undefined local variable or method `user' for main:Object
from (irb):7
注意:我输入了User.new
,输入了姓名和电子邮件,然后user.save
,得到了上面的错误。
完整代码:
C:\Sites\rails_projects\sample_app>bundle exec rake db:migrate
== CreateUsers: migrating ====================================================
-- create_table(:users)
-> 0.0020s
== CreateUsers: migrated (0.0020s) ===========================================
C:\Sites\rails_projects\sample_app>rails console --sandbox
Loading development environment in sandbox (Rails 4.0.1)
Any modifications you make will be rolled back on exit
irb(main):001:0> User.new
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil>
<ame: "Andrew Ghobrial", email: "email@gmail.com")
=> #<User id: nil, name: "Andrew Ghobrial", email: "email@gmail.com",
created_at: nil, updated_at: nil>
irb(main):004:0> user.save
NameError: undefined local variable or method `user' for main:Object
from (irb):4
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
irb(main):006:0> user.save
NameError: undefined local variable or method `user' for main:Object
from (irb):6
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
irb(main):007:0> user
NameError: undefined local variable or method `user' for main:Object
from (irb):7
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'`