Datamapper can't update password token

23 views Asked by At

I have a user model

class User
  include Datamapper::Resource

  property :id, Serial
  property :password_token, String

  def generate_token
   self.password_token = SecureRandom.hex
    self.save
   end
end

and this controller route:

post '/users/token_sent' do
 user = User.first(email: params[:email])
   if user
     user.generate_token 
   end
 redirect('index')
end

I would like the password_token property to be changed to the SecureRandom.hex instance when the user fills in an email to recover their password from the post route.

The method does not update the password_token to the Users database.

1

There are 1 answers

0
max pleaner On

In this method:

   def generate_token
     self.password_token = SecureRandom.hex
     self.save
   end

You're calling self.save, which isn't guaranteed to succeed.

If you want to know why it's failing to save, use save! instead.