Ruby on Rails: undefined method 'new_token' for user
I am following Michael Hartley Ruby on the Rails Tutorial and I came up with an error that I cannot figure out.
I keep getting the following error: NoMethodError: undefined method 'new_token' for user. This happens when I just try to instantiate the user or if I just call the user from the console
The code below is stubbing as I was trying to isolate the problem by commenting out some of the other methods.
class User < ActiveRecord::Base
attr_accessor :remember_token, :activation_token, :reset_token
validates :name, presence: true, length: {maximum: 50}
validates :email, presence: true, length: {maximum: 255}, format: {with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i}, uniqueness: {case_sensitive: false}
validates :password, presence: true, length: {minimum: 6}, allow_nil: true
has_secure_password
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
def User.new_token
SecureRandom.urlsafe_base64
end
end
When I try something just in the console, like returning the first user, I get this error:
2.2.0 :019 > User.first
NoMethodError: undefined method `new_token' for User (call 'User.connection' to establish a connection):Class
from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/activerecord-4.2.0/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
from /Users/Jshyu/Desktop/Jitter/app/models/user.rb:15:in `<class:User>'
from /Users/Jshyu/Desktop/Jitter/app/models/user.rb:2:in `<top (required)>'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `load'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `block in load_file'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:647:in `new_constants_in'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:456:in `load_file'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:354:in `require_or_load'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:494:in `load_missing_constant'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:184:in `const_missing'
from (irb):19
from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /Users/Jshyu/Desktop/Jitter/bin/rails:8:in `<top (required)>'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /Users/Jshyu/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/Jshyu/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'2.2.0 :020 >
EDIT:
What's even weirder, when I comment out the self.new_token class method, I get another error, NameError: undefined local variable or method '' for user
class User < ActiveRecord::Base
attr_accessor :remember_token, :activation_token, :reset_token
validates :name, presence: true, length: {maximum: 50}
validates :email, presence: true, length: {maximum: 255}, format: {with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i}, uniqueness: {case_sensitive: false}
validates :password, presence: true, length: {minimum: 6}, allow_nil: true
has_secure_password
#Returns the hash digest of the given string
def User.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
# def self.new_token
# SecureRandom.urlsafe_base64
# end
end
2.2.0 :026 > User
NameError: undefined local variable or method ` ' for User (call 'User.connection' to establish a connection):Class
from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/activerecord-4.2.0/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
from /Users/Jshyu/Desktop/Jitter/app/models/user.rb:22:in `<class:User>'
from /Users/Jshyu/Desktop/Jitter/app/models/user.rb:2:in `<top (required)>'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `load'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:457:in `block in load_file'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:647:in `new_constants_in'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:456:in `load_file'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:354:in `require_or_load'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:494:in `load_missing_constant'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:184:in `const_missing'
from (irb):26
from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0@global/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:274:in `require'
from /Users/Jshyu/Desktop/Jitter/bin/rails:8:in `<top (required)>'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/Jshyu/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /Users/Jshyu/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/Jshyu/.rvm/rubies/ruby-2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'2.2.0 :027 >
I recently installed sublimelinter and rubocop, but after uninstalling them ...
source to share
No one has answered this question yet
Check out similar questions: