Ruby on Rails 3 教程:在控制台中输入命令问题

发布于 2024-12-23 12:54:19 字数 1052 浏览 2 评论 0原文

我正在学习本教程,但遇到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2024-12-30 12:54:19

如果我没记错的话,你不能小写类名。

尝试将类重命名为 User

以下内容应该有效:

class User < ActiveRecord::Base
  attr_accessible :name, :email
  validates :name, :presence => true
end

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:

class User < ActiveRecord::Base
  attr_accessible :name, :email
  validates :name, :presence => true
end

rails c

User.new

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文