1
votes

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:

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.

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>'`
4
post the code that gives this error - tihom
I typed in User.new, input a name and email, then input user.save, and got the error above. - Andrew
you started with user = User.new, right? - Phlip
I tried that, and it gave me the error: 'user' is not recognized as an internal or external command, operable program or batch file. - Andrew
Can you post every line you ran in IRB, exactly as you did it? - CDub

4 Answers

3
votes

So, as @Phlip said, your User.new isn't assigned to a variable. If you do:

user = User.new(name: "Andrew Ghobrial", email: "[email protected]")

user.save

That will work, assuming your users table has name and email columns, and your user model looks something like:

class User << ActiveRecord::Base

  attr_accessor :name, :email

end
1
votes

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:

rails g resource user

After the db migration:

Typing "user.count" in IRB gets the NameError

Typing "User.count" gets the expected result (for my empty table):

    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.

0
votes

1.9.3-p392 :004 > status.delete_all

in this Status 's' needs to be capitalized to 'S'.

or

in this case

NameError: undefined local variable or method `user' for main:Object

in this Status 'u' needs to be capitalized to 'U'.

or

in this case

NameError: undefined local variable or method `post' for main:Object

in this Status 'p' needs to be capitalized to 'P'.

-3
votes

Make sure that in (Resourse).user, The first letter (pin = Pin, or tweet = Tweet) is capitalized.