轨控制台--sandbox迈克尔·哈特尔的轨道教程第三版不知道这是如何工作
我使用有一个问题与Ruby on Rails的:轨控制台--sandbox迈克尔·哈特尔的轨道教程第三版不知道这是如何工作
rails console --sandbox
所以在迈克尔·哈特尔的轨道教程第3版,我们正在使用此,我当我有时候使用它,并且一切顺利,然后出现错误,并且从字面上看,整个测试环境变得无用,因为它不会响应我的命令。例如,我将键入:
2.1.1 :025 > user.first
,或者是工作,应该是工作,而是得到任何其他命令:
NoMethodError: undefined method `first' for #<User:0x007fede35e3188>
from /Users/andrewkim/.rvm/gems/[email protected]_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/[email protected]_rails_4.2.0.beta2 gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:110:in `start'
from /Users/andrewkim/.rvm/gems/[email protected]_rails_4.2.0.beta2 gems/railties-4.2.0.beta2/lib/rails/commands/console.rb:9:in `start'
from /Users/andrewkim/.rvm/gems/[email protected]_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/[email protected]_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/[email protected]_rails_4.2.0.beta2/gems/railties-4.2.0.beta2/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/andrewkim/.rvm/gems/[email protected]_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:248:in `require'
from /Users/andrewkim/.rvm/gems/[email protected]_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/[email protected]_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/[email protected]_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/[email protected]_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /Users/andrewkim/.rvm/gems/[email protected]_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/[email protected]_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/[email protected]_rails_4.2.0.beta2/gems/activesupport-4.2.0.beta2/lib/active_support/dependencies.rb:242:in `load'
from /Users/andrewkim/.rvm/gems/[email protected]_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/commands/rails.rb:6:in `call'
from /Users/andrewkim/.rvm/gems/[email protected]_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/command_wrapper.rb:38:in `call'
from /Users/andrewkim/.rvm/gems/[email protected]_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:180:in `block in serve'
from /Users/andrewkim/.rvm/gems/[email protected]_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:153:in `fork'
from /Users/andrewkim/.rvm/gems/[email protected]_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:153:in `serve'
from /Users/andrewkim/.rvm/gems/[email protected]_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
from /Users/andrewkim/.rvm/gems/[email protected]_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
from /Users/andrewkim/.rvm/gems/[email protected]_rails_4.2.0.beta2/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
from /Users/andrewkim/.rvm/gems/[email protected]_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 >
突然什么,我在终端上键入将带着这样的错误,我不知道怎么去无需退出“rails console --sandbox”。有没有办法?
for example, I will type:
2.1.1 :025 > user.first
or any other command that was working and should be working
该命令不应该工作,它不起作用。您遇到错误是因为在教程中有一个User实例,它表示表中的一行,例如
user = User.new(name: "Joe", email: "[email protected]")
没有名为first()
的方法。相反,用户实例具有以下方法:
id()
name()
email()
created_at()
updated_at()
然而,可以使用类名称(或型号名称)来搜索表格,例如,
user1 = User.first. #User is the name of the class/model
至于这样的:
and I don't know how to go back
回去呢?什么也没有变。在错误发生之前,您输入到控制台的所有变量仍然存在。这里是一个轨道控制台的例子:
$ 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: "[email protected]")
User Load (4.0ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
=> #<User id: 2, name: "Example User", email: "[email protected]", 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/[email protected]_app2_gems/gems/activemodel-4.0.8/lib/active_model/attribute_methods.rb:439:in `method_missing'
from /Users/7stud/.rvm/gems/[email protected]_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/[email protected]_app2_gems/gems/railties-4.0.8/lib/rails/commands/console.rb:90:in `start'
from /Users/7stud/.rvm/gems/[email protected]_app2_gems/gems/railties-4.0.8/lib/rails/commands/console.rb:9:in `start'
from /Users/7stud/.rvm/gems/[email protected]_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: "[email protected]", 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
这应该是更容易你去调试。
感谢您的回复。我一直忙于工作,而且我没有改变回到网上,所以我对迟到的回应表示歉意。因此,这完全回答了我的问题的第一部分,因为第二部分我非常含糊。让我改述一下。所以我输入了一个错误的命令到终端,但后来我得到了错误信息(上面粘贴),我无法在终端输入任何内容。我不得不完全退出终端标签。我注意到的不同之处在于缩进(tabbed),当控制台工作时它不是。 – 2014-10-21 04:58:53
@AndrewK,如果终端完全没有响应,我想无论是否有缩进都没关系。否则,您可以键入'end'语句以返回到缩进的外层。但是输入一个不正确的命令,比如'user.first',不应该改变缩进级别。你能像我一样发布你的rails控制台输出的例子,所以我可以看到你看到的是什么? – 7stud 2014-10-21 18:36:24
顺便说一句,这是章节6.1.3:创建用户对象 – 2014-10-18 22:47:49
难道你不想使用'User.first'? (大写“U”) – Doguita 2014-10-19 00:58:18
@matthias_h:上下文:[拒绝编辑后跟几乎相同的编辑](http://meta.*.com/q/274790) – 2014-10-19 01:47:18