NameError:main:Object的未定义局部变量或方法“ user”

安德鲁

我遵循了Ruby on Rails教程,并且尝试在Rails Console(沙盒模式)中保存用户时,出现以下错误:

NameError: undefined local variable or method `user' for main:Object
from (irb):7

注意:我输入User.new,输入名称和电子邮件,然后输入user.save,并得到上面的错误。

完整代码:

C:\Sites\rails_projects\sample_app>bundle exec rake db:migrate
==  CreateUsers: migrating ====================================================
-- create_table(:users)
   -> 0.0020s
==  CreateUsers: migrated (0.0020s) ===========================================


C:\Sites\rails_projects\sample_app>rails console --sandbox
Loading development environment in sandbox (Rails 4.0.1)
Any modifications you make will be rolled back on exit
irb(main):001:0> User.new
=> #<User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil>
<ame: "Andrew Ghobrial", email: "[email protected]")
=> #<User id: nil, name: "Andrew Ghobrial", email: "[email protected]",
created_at: nil, updated_at: nil>
irb(main):004:0> user.save
NameError: undefined local variable or method `user' for main:Object
        from (irb):4
        from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
        from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
        from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'
irb(main):006:0> user.save
NameError: undefined local variable or method `user' for main:Object
        from (irb):6
        from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
        from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
        from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'
irb(main):007:0> user
NameError: undefined local variable or method `user' for main:Object
        from (irb):7
        from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
        from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
        from C:/RailsInstaller/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems
/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'`
光盘

因此,正如@Phlip所说,您User.new没有分配给变量。如果您这样做:

user = User.new(name: "Andrew Ghobrial", email: "[email protected]")

user.save

假设您的用户表具有nameemail列,并且您的用户模型如下所示,那将起作用

class User << ActiveRecord::Base

  attr_accessor :name, :email

end

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

NameError - #<Sinatra::Application:0x0000000381bde0> 的未定义局部变量或方法“user_details”:

来自分类Dev

未定义的局部变量或方法`user_params'rails 4

来自分类Dev

#<UsersController:0x0000000570eef0>的未定义局部变量或方法'user'

来自分类Dev

未定义的局部变量或方法“ current_user”可以

来自分类Dev

未定义的局部变量或方法`current_user'cancancan

来自分类Dev

为订单使用未定义的局部变量或方法“ current_user”

来自分类Dev

main:Object(NameError)的未定义局部变量或方法`config'

来自分类Dev

main:Object(NameError)的未定义局部变量或方法“类别”

来自分类Dev

main:Object(NameError)的未定义局部变量或方法'res'

来自分类Dev

NameError:main:Object的未定义局部变量或方法“ request”

来自分类Dev

main:Object (NameError) 的未定义局部变量或方法“api”

来自分类Dev

Rails Merit 未定义局部变量或方法 `user' for #<Merit::BadgeRules:0x0000000415b5e0>

来自分类Dev

main:Object的未定义局部变量或方法`'

来自分类Dev

自制程序安装问题:在“ <main>”中:main:Object(NameError)的未定义局部变量或方法“”

来自分类Dev

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

来自分类Dev

NameError未定义的局部变量或方法“ drink”

来自分类Dev

未定义的局部变量或方法“ main_page”

来自分类Dev

deploy.rb:main:Object的未定义局部变量或方法“ home”

来自分类Dev

Rails:main:Object的未定义局部变量或方法“ signup_path”

来自分类Dev

未定义的局部变量或方法

来自分类Dev

NameError:未定义的局部变量或扫描方法上的方法

来自分类Dev

行动邮件程序NameError:未定义的局部变量或方法“ smtp”

来自分类Dev

Ruler :: Application:Class的机架未定义局部变量或方法`env'(NameError)

来自分类Dev

Rails视图中的binding.pry:NameError:未定义的局部变量或方法

来自分类Dev

error_message 部分获取 NameError: 未定义的局部变量或方法“对象”

来自分类Dev

main:Object(NameError)的局部变量或方法“ config”

来自分类Dev

Rails:未定义的本地变量或方法“ current_user”

来自分类Dev

未定义的局部变量或模块的方法

来自分类Dev

未定义的局部变量或方法“ place”

Related 相关文章

  1. 1

    NameError - #<Sinatra::Application:0x0000000381bde0> 的未定义局部变量或方法“user_details”:

  2. 2

    未定义的局部变量或方法`user_params'rails 4

  3. 3

    #<UsersController:0x0000000570eef0>的未定义局部变量或方法'user'

  4. 4

    未定义的局部变量或方法“ current_user”可以

  5. 5

    未定义的局部变量或方法`current_user'cancancan

  6. 6

    为订单使用未定义的局部变量或方法“ current_user”

  7. 7

    main:Object(NameError)的未定义局部变量或方法`config'

  8. 8

    main:Object(NameError)的未定义局部变量或方法“类别”

  9. 9

    main:Object(NameError)的未定义局部变量或方法'res'

  10. 10

    NameError:main:Object的未定义局部变量或方法“ request”

  11. 11

    main:Object (NameError) 的未定义局部变量或方法“api”

  12. 12

    Rails Merit 未定义局部变量或方法 `user' for #<Merit::BadgeRules:0x0000000415b5e0>

  13. 13

    main:Object的未定义局部变量或方法`'

  14. 14

    自制程序安装问题:在“ <main>”中:main:Object(NameError)的未定义局部变量或方法“”

  15. 15

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

  16. 16

    NameError未定义的局部变量或方法“ drink”

  17. 17

    未定义的局部变量或方法“ main_page”

  18. 18

    deploy.rb:main:Object的未定义局部变量或方法“ home”

  19. 19

    Rails:main:Object的未定义局部变量或方法“ signup_path”

  20. 20

    未定义的局部变量或方法

  21. 21

    NameError:未定义的局部变量或扫描方法上的方法

  22. 22

    行动邮件程序NameError:未定义的局部变量或方法“ smtp”

  23. 23

    Ruler :: Application:Class的机架未定义局部变量或方法`env'(NameError)

  24. 24

    Rails视图中的binding.pry:NameError:未定义的局部变量或方法

  25. 25

    error_message 部分获取 NameError: 未定义的局部变量或方法“对象”

  26. 26

    main:Object(NameError)的局部变量或方法“ config”

  27. 27

    Rails:未定义的本地变量或方法“ current_user”

  28. 28

    未定义的局部变量或模块的方法

  29. 29

    未定义的局部变量或方法“ place”

热门标签

归档