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

时间:2013-11-08 19:46:35

标签: ruby-on-rails ruby database console

我正在关注Ruby on Rails教程,当尝试在Rails控制台(沙盒模式)中保存用户时,我收到以下错误:

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@gmail.com")
=> #<User id: nil, name: "Andrew Ghobrial", email: "email@gmail.com",
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>'`

4 个答案:

答案 0 :(得分:3)

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

user = User.new(name: "Andrew Ghobrial", email: "email@gmail.com")

user.save

如果您的用户表包含nameemail列,那么这将有效,您的用户模型如下所示:

class User << ActiveRecord::Base

  attr_accessor :name, :email

end

答案 1 :(得分:1)

虽然大写答案尚未被接受,但IRB最初帖子的第一行证明了这是问题所在。我的资源是用小写的标题创建的:

rails g resource user

数据库迁移后:

在IRB中键入“ user.count”会得到NameError

键入“ U ser.count”可获得预期的结果(对于我的空表):

    irb(main):009:0> User.count
   (0.4ms)  SELECT COUNT(*) FROM "user"
    => 0

因此,IRB对活动资源区分大小写,并且似乎必须在声明中将其大写。键入“用户”将返回字段/列名称的列表。

答案 2 :(得分:0)

1.9.3-p392:004&gt; status.delete_all

在这个状态&#39;需要资本化为&#39;

在这种情况下

NameError:未定义的局部变量或方法`user&#39; for main:Object

在此状态&#39; u&#39;需要资本化为&#39; U&#39;

在这种情况下

NameError:未定义的局部变量或方法`post&#39; for main:Object

在此状态&#39; p&#39;需要资本化为P&#39;。

答案 3 :(得分:-3)

确保在(Resourse).user中,第一个字母(pin = Pin, or tweet = Tweet)大写。