我目前正在关注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)',现在你有一个用户执行某些操作 –