I've been trying to add a password reset for users that forget their password. The users clicks on forgot password? on sign up page. Then the user types their email and clicks reset password, which creates a token and sends an email with a link to reset their password. For the most part, it was working well, and then it suddenly stopped working. When a user clicks password reset, it brings up the error message: Password cant be blank, password is too short(6 min)
Ran into this error in video 275 How I Test. on 11:20
Failure/Error: click_button "Reset Password"
ActiveRecord::RecordInvalid:
Validation failed: Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank
# ./app/models/user.rb:30:in send_password_reset'
# ./app/controllers/password_resets_controller.rb:7:increate'
# (eval):2:in click_button'
# ./spec/requests/password_resets_spec.rb:9:inblock (2 levels) in '
Finished in 13.66 seconds
95 examples, 1 failure
This is some of the code being used.
user.rb
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
def send_password_reset
generate_token(:password_reset_token)
self.password_reset_sent_at = Time.zone.now
save!
UserMailer.password_reset(self).deliver
end
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
password_resets_controller.rb
class PasswordResetsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:email])
user.send_password_reset
redirect_to root_url, :notice => "Email sent with password reset instructions."
end
def edit
@user = User.find_by_password_reset_token!(params[:id])
end
end
new.html.erb
<h1>Reset Password</h1>
<%= form_tag password_resets_path, :method => :post do %>
<div class="field">
<%= label_tag :email %>
<%= text_field_tag :email, params[:email] %>
</div>
<div class="actions"><%= submit_tag "Reset Password" %></div>
<% end %>