Adnan Pirota
Adnan Pirota

Reputation: 309

Undefined method 'remember_token=" while following railsturial.org

I have seen several questions regarding this issue bot none of them helped me. So the problem is, while executing if I check remember me I get this error:

"undefined method `remember_token=' for #"

relevant code:

User.rb

#Returns a random token.
def User.new_token
  SecureRandom.urlsafe_base64
end
# Rememember a user in the database for use in persistent sessions.
 def remember 
   self.remember_token = User.new_token **# This is the row 30 where the execution stops**
   update_attribute(:remember_digest, User.digest(remember_token))
end

I have checked the database has got the column remember_digest so it's not the database column.

This code is being called from session_helper.rb i.e. from code:

# Remembers a user in a persistent session.
def remember(user)
  user.remember 
  cookies.permanent[:remember_token] = user.remember_token
  cookies.permament.signed[:user_id] = user.id
end

Also in my console if I type User.new_token the console return new token without a problem.

Upvotes: 2

Views: 1261

Answers (3)

mikias
mikias

Reputation: 446

Hey I was struggling with this problem and in the end I found Naga Sudhir's answer solved the issue.

It turns out I didn't understand that attr_accessor :remember_token was actually a line of code I had to enter in the User models class (user.rb).

class User < ActiveRecord::Base
  attr_accessor :remember_token
  before_save { self.email = email.downcase }
  validates :first_name, presence: true, length: { maximum: 25}
  validates :last_name, presence: true, length: { maximum: 25 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255}, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
  validates :password, length: { minimum: 6 }
  has_secure_password

That allows self.remember_token to be used later in the code.

Hope that clears it up and helps anyone else struggling through the tutorial. :)

Upvotes: 0

Naga Sudhir
Naga Sudhir

Reputation: 51

I too was stuck with this problem while practising the rails tutorial book.

The mistake I did was that I forgot to write attr_accessor :remember_token which was the reason for the variable not being accessed by the remember(user) method.

I was also frustrated with this problem but luckily I found the solution.

PS: Check If you wrote attr_accessor :remember_token in the User model's class (user.rb)

Upvotes: 1

Mandeep
Mandeep

Reputation: 9173

Error: "undefined method `remember_token='

You are getting this error because users table doesn't have a remember_token field.

I have checked the database has got the column remember_digest so it's not the database column.

When you use self.remember_token = User.new_token, you are trying to set remember_token field in users table with output of new_token method. Why do you think you need to have remember_digest field in users table when you are assigning it to remember_token?

FIX

If you want to implement remember me functionality then you can follow this railscast. Your code would be something like this:

#User.rb
#Returns a random token.
def new_token
  SecureRandom.urlsafe_base64
end
# Rememember a user in the database for use in persistent sessions.
def remember
  self.update_attribute(:remember_digest, new_token)
end

# Remembers a user in a persistent session.
def remember(user)
  user.remember 
  cookies.permanent[:remember_token] = user.remember_digest
  cookies.permament.signed[:user_id] = user.id
end

P.S I'm using remember_digest as it's already in your users table. You can change it and use a more descriptive field like remember_token

Upvotes: 1

Related Questions