我对Ruby on Rails有一个问题:
rails console --sandbox
所以在迈克尔·哈特尔的第三版轨道教程中,我们正在使用它,有时我会使用它并且一切顺利,然后我有一个错误,并且字面上是整个测试我遇到了问题环境变得无用,因为它不会响应我的命令
例如,我将输入:
2.1.1 :025 > user.first
或任何其他正在运行且应该正在运行的命令,而不是:
NoMethodError: undefined method `first' for #<User:0x007fede35e3188>
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/activemodel-4.2.0.beta2/lib/active_model/attribute_methods.rb:435:in `method_missing' from (irb):25
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2 gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:110:in `start'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2 gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:9:in `start'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/railties-4.2.0.beta2/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/railties-4.2.0.beta2/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `block in require'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /Users/andrewkim/workspace2/sample_app/bin/rails:8:in `<top (required)>'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `block in load'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:233:in `load_dependency'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/commands/rails.rb:6:in `call'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/command_wrapper.rb:38:in `call'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:180:in `block in serve'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:153:in `fork'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:153:in `serve'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /Users/andrewkim/.rvm/gems/ruby-2.1.1@railstutorial_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/andrewkim/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/andrewkim/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'2.1.1 :026 >
突然间,我在终端输入的任何内容都会出现这样的错误,而且我不知道怎么回去而没有退出&#34; rails console --sandbox&#34;。有办法吗?
答案 0 :(得分:3)
例如,我将输入:2.1.1 :025 > user.first
或任何其他正在运行且应该正常工作的命令
这个命令不应该起作用,而且它不起作用。您收到错误,因为在教程中有一个User实例,它表示表中的一行,例如
user = User.new(name: "Joe", email: "joe@yahoo.com")
没有名为first()
的方法。相反,用户实例具有以下方法:
id()
name()
email()
created_at()
updated_at()
但是,类名(或型号名称)可用于搜索表格,例如
user1 = User.first. #User is the name of the class/model
至于此:
我不知道怎么回去
回到什么地方?什么也没有变。在该错误之前键入控制台的所有变量仍然存在。以下是rails控制台的示例:
$ rails console --sandbox
Loading development environment in sandbox (Rails 4.0.8)
Any modifications you make will be rolled back on exit
2.0.0-p481 :001 > x = 10
=> 10
2.0.0-p481 :002 > user = User.find_by(email: "example@railstutorial.org")
User Load (4.0ms) SELECT "users".* FROM "users" WHERE "users"."email" = 'example@railstutorial.org' LIMIT 1
=> #<User id: 2, name: "Example User", email: "example@railstutorial.org", created_at: "2014-10-02 01:39:04", updated_at: "2014-10-02 01:39:04", password_digest: "$2a$10$VNzivk5opu1AC5DOM467dO2JaTg3c3JC9OAXR0AqvEi0...">
2.0.0-p481 :003 > user.first
NoMethodError: undefined method `first' for #<User:0x00000100f360c8>
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481@sample_app2_gems/gems/activemodel-4.0.8/lib/active_model/attribute_methods.rb:439:in `method_missing'
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481@sample_app2_gems/gems/activerecord-4.0.8/lib/active_record/attribute_methods.rb:168:in `method_missing'
from (irb):3
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481@sample_app2_gems/gems/railties-4.0.8/lib/rails/commands/console.rb:90:in `start'
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481@sample_app2_gems/gems/railties-4.0.8/lib/rails/commands/console.rb:9:in `start'
from /Users/7stud/.rvm/gems/ruby-2.0.0-p481@sample_app2_gems/gems/railties-4.0.8/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.0.0-p481 :004 > user
=> #<User id: 2, name: "Example User", email: "example@railstutorial.org", created_at: "2014-10-02 01:39:04", updated_at: "2014-10-02 01:39:04", password_digest: "$2a$10$VNzivk5opu1AC5DOM467dO2JaTg3c3JC9OAXR0AqvEi0...">
2.0.0-p481 :005 > x
=> 10
2.0.0-p481 :006 >
请参阅?一切都还在那里。另一方面,如果"go back"
表示"go back to the blissful state you were in when you got no errors"
,那么解决方法是在控制台中输入正确的命令。下一次,从您尝试模拟的教程中复制命令并将其粘贴到空白文本文件中,然后复制导致错误的命令(但您知道这是正确的!)并将其粘贴到命令之下教程:
User.first
user.first
然后比较它们。从不创建与模型同名的变量,例如,您的问题也可以有所缓解。而不是写作:
user = User.new(...)
写:
my_user = User.new(...)
然后如果你写:
user.first
您将收到错误:
NameError: undefined local variable or method `user' for main:Object
应该更容易调试。