Ruby on Rails 3 教程:在控制台中输入命令问题
我正在学习本教程,但遇到了 Rails 控制台无法识别我创建的用户模型的问题。这是我正在使用的内容
是我在用户模型 user.rb
中定义的内容,位于 sample_app/app/models/user.rb
中:
class user < ActiveRecord::Base
attr_accessible :name, :email
validates :name, :presence => true
end
这 当我尝试引用它时,我在控制台中得到的是:
Nicholass-MacBook-Pro:sample_app nbkincaid$ rails console
Loading development environment (Rails 3.1.1)
ruby-1.9.2-p290 :001 > user.new
NameError: undefined local variable or method `user' for main:Object from (irb):1
from /Users/nbkincaid/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
from /Users/nbkincaid/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
from /Users/nbkincaid/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
是否有我需要遵守的特定 gem 版本集,或者是否还有其他我缺少的东西?我不太明白这一点。
I am working through this tutorial book and have run into an issue with the rails console not recognizing the user model that I've created. Here is what I am working with
This is what I've defined in my user model, user.rb
, located in sample_app/app/models/user.rb
:
class user < ActiveRecord::Base
attr_accessible :name, :email
validates :name, :presence => true
end
This is what I get in console when I try to reference it:
Nicholass-MacBook-Pro:sample_app nbkincaid$ rails console
Loading development environment (Rails 3.1.1)
ruby-1.9.2-p290 :001 > user.new
NameError: undefined local variable or method `user' for main:Object from (irb):1
from /Users/nbkincaid/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
from /Users/nbkincaid/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
from /Users/nbkincaid/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Is there a specific gem version set that I need to adhere to, or is there something else that I am missing? I can't quite figure this one out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没记错的话,你不能小写类名。
尝试将类重命名为
User
以下内容应该有效:
rails c
User.new
You can't lowercase a class name if I recall correctly.
Try renaming the class to
User
The following should work:
rails c
User.new