2014-10-18 68 views
0

我使用有一個問題與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」。有沒有辦法?

+0

順便說一句,這是章節6.1.3:創建用戶對象 – 2014-10-18 22:47:49

+0

難道你不想使用'User.first'? (大寫「U」) – Doguita 2014-10-19 00:58:18

+0

@matthias_h:上下文:[拒絕編輯後跟幾乎相同的編輯](http://meta.stackoverflow.com/q/274790) – 2014-10-19 01:47:18

回答

3

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 

這應該是更容易你去調試。

+0

感謝您的回覆。我一直忙於工作,而且我沒有改變回到網上,所以我對遲到的迴應表示歉意。因此,這完全回答了我的問題的第一部分,因爲第二部分我非常含糊。讓我改述一下。所以我輸入了一個錯誤的命令到終端,但後來我得到了錯誤信息(上面粘貼),我無法在終端輸入任何內容。我不得不完全退出終端標籤。我注意到的不同之處在於縮進(tabbed),當控制檯工作時它不是。 – 2014-10-21 04:58:53

+0

@AndrewK,如果終端完全沒有響應,我想無論是否有縮進都沒關係。否則,您可以鍵入'end'語句以返回到縮進的外層。但是輸入一個不正確的命令,比如'user.first',不應該改變縮進級別。你能像我一樣發佈你的rails控制檯輸出的例子,所以我可以看到你看到的是什麼? – 7stud 2014-10-21 18:36:24

相關問題