我目前正在關注YouTube上的recipe box教程。 當我打開軌道控制檯,然後輸入Ruby on Rails中的局部變量未定義
@recipe = current_user.recipes.build
我得到了以下錯誤:
NameError: undefined local variable or method `current_user' for main:Object
from (irb):3
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/matthiascordes/.rvm/gems/ruby-2.3.1/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:9:in `require'
from bin/rails:9:in `<main>'
有誰有一個想法,什麼是錯的,我怎麼能解決這個問題? 我正在爲我的用戶模型使用設計寶石。
好的,我會試試看。我在哪裏必須添加這些行? –
在你的rails控制檯裏面,因爲你試圖訪問控制檯內的current_user,因爲你需要一個用戶來執行一些操作,例如你可以這樣做,'user = User.find(id)',現在你有一個用戶執行某些操作 –