Ruby on Rails中的局部变量未定义

马蒂亚斯·科德斯(Matthias Cordes)

我目前正在youtube上关注食谱框教程。当我打开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>'

是否有人知道哪里出了问题以及如何解决这个问题?我正在将devise gem用于我的用户模型。

拉维·玛丽亚(Ravi mariya)

您无法current_user在Rails控制台中访问,因此请尝试

user = User.find(your_user_id)
user.recipes.build

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Ruby on Rails呈现部分未定义的局部变量

Ruby on Rails的未定义局部变量或方法`profile'

Ruby on Rails错误:未定义的局部变量或方法

基于Ruby中语法的未定义局部变量

Ruby 需要导致未定义的局部变量

Ruby编程错误(未定义的局部变量)

未定义的局部变量或带有response_with的方法(Ruby on Rails)

NameError:未定义-在Ruby 2.1.2中是否更改了局部变量的解析规则?

外部文件中的未定义局部变量(“为什么要对Ruby进行Poignant指南”)

定义了一个Ruby NameError,未定义的局部变量

Git Shell Ruby Bundler错误解析“ Gemfile”:未定义的局部变量或方法“ ?? g”

在Ruby on Rails应用中使用JSON将数据从Googlesheets解析为FusionCharts时使用未定义的局部变量或方法`parsedData'

ruby on rails-Images#show中的NameError,#<#<Class:0x007f2179a53d30>:0x007f2174b50238>的未定义局部变量或方法`image'

Ruby查找需要上传哪些文件或目录:main:Object(NameError)的未定义局部变量或方法“ dir”

在Rails中未定义的局部变量或方法to_a?

Rails / Ruby错误地显示了未定义的变量

Ruby 中的线程局部实例局部变量

如果未定义,则在Ruby中定义变量(不是|| =)

Rails形式的未定义局部变量

Rails:未定义的局部变量或方法`options'

Rails Ajax未定义的局部变量或方法

rails:未定义的局部变量或方法“请求”

Ruby def 函数无法定义局部变量

Rspec测试中未定义的局部变量或方法?

角度订阅在局部变量中未定义

在我的主中未定义局部变量

部分中的未定义局部变量

Rails 4:OldImagePath模块中的未定义局部变量或方法“ asset_paths”

人偶-未定义的局部变量