Ruby-on-rails NameError:main:Object 的未定义局部变量或方法“用户”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19867354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NameError: undefined local variable or method `user' for main:Object
提问by Andrew
I'm following through the Ruby on Rails tutorial, and when attempting to save a user in Rails Console (sandbox mode), I get the following error:
我正在学习 Ruby on Rails 教程,当尝试在 Rails 控制台(沙箱模式)中保存用户时,出现以下错误:
NameError: undefined local variable or method `user' for main:Object
from (irb):7
Note: I typed in User.new, input a name and email, then user.save, and got the error above.
注意:我输入了User.new,输入了姓名和电子邮件,然后是user.save,然后出现了上面的错误。
Full code:
完整代码:
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>'`
回答by CDub
So, as @Phlip said, your User.newisn't assigned to a variable. If you do:
因此,正如@Phlip 所说,您User.new没有分配给变量。如果你这样做:
user = User.new(name: "Andrew Ghobrial", email: "[email protected]")
user.save
That will work, assuming your users table has nameand emailcolumns, and your user model looks something like:
这将起作用,假设您的用户表具有name和email列,并且您的用户模型如下所示:
class User << ActiveRecord::Base
attr_accessor :name, :email
end
回答by Remy
While the capitalisation answers haven't been upvoted, the very first line of the original post IRB proves this was the issue. My resource was created with a lowercase title:
虽然大写答案尚未被赞成,但 IRB 原始帖子的第一行证明这是问题所在。我的资源是用小写标题创建的:
rails g resource user
After the db migration:
数据库迁移后:
Typing "user.count" in IRB gets the NameError
在 IRB 中输入“user.count”得到 NameError
Typing "User.count" gets the expected result (for my empty table):
键入“ üser.count”得到预期的结果(我的空表):
irb(main):009:0> User.count
(0.4ms) SELECT COUNT(*) FROM "user"
=> 0
So the IRB is case sensitive on active resources and seemingly must be capitalised in declarations. Typing "User" will return a list of the field/column names.
因此,IRB 对活动资源区分大小写,并且似乎必须在声明中大写。键入“用户”将返回字段/列名称的列表。
回答by Bharathi
1.9.3-p392 :004 > status.delete_all
1.9.3-p392 :004 > status.delete_all
in this Status 's' needs to be capitalized to 'S'.
在此状态中,“s”需要大写为“S”。
or
或者
in this case
在这种情况下
NameError: undefined local variable or method `user' for main:Object
NameError:main:Object 的未定义局部变量或方法“用户”
in this Status 'u' needs to be capitalized to 'U'.
在这种状态下,'u' 需要大写为 'U'。
or
或者
in this case
在这种情况下
NameError: undefined local variable or method `post' for main:Object
NameError:main:Object 的未定义局部变量或方法“post”
in this Status 'p' needs to be capitalized to 'P'.
在此状态中,“p”需要大写为“P”。
回答by Code Oga
Make sure that in (Resourse).user, The first letter (pin = Pin, or tweet = Tweet)is capitalized.
确保在(Resourse).user, 第一个字母(pin = Pin, or tweet = Tweet)大写。

