2

我正在阅读 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

4 回答 4

3

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

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

user.save

这将起作用,假设您的 users 表有nameemail列,并且您的用户模型看起来像:

class User << ActiveRecord::Base

  attr_accessor :name, :email

end
于 2013-11-08T20:46:44.420 回答
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 对活动资源是区分大小写的,并且在声明中似乎必须大写。键入“用户”将返回字段/列名称的列表。

于 2019-10-01T22:37:11.930 回答
0

1.9.3-p392:004 > status.delete_all

在此状态中,“s”需要大写为“S”。

或者

在这种情况下

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

在此状态中,“u”需要大写为“U”。

或者

在这种情况下

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

在此状态中,“p”需要大写为“P”。

于 2015-04-04T04:49:59.323 回答
-3

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

于 2014-08-24T07:39:24.987 回答