Вопрос

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.

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top