就是密碼驗(yàn)證那一塊出了問(wèn)題(即密碼不符)
我重新寫過(guò)這一塊,以及Google找了相關(guān)的實(shí)例還是會(huì)報(bào)錯(cuò),希望能解決,謝謝。
這個(gè)專案來(lái)自我學(xué)習(xí)的網(wǎng)站:https://www.railstutorial.org...
PS:目前懷疑是<%= render 'shared/error_messages' %>
(edit.html.erb)這條程序沒(méi)起作用,不過(guò)「註冊(cè)用戶」也是用的同樣的方法,能正常驗(yàn)證密碼和郵箱的格式。但是「重置密碼」就不起作用了。
實(shí)際操作中可以分辨空值、密碼過(guò)短。但輸入兩個(gè)不同的密碼就直接修改成功為第一次輸入的密碼了。
app/views/password_resets/edit.html.erb
<% provide(:title, 'Reset password') %>
<h1>Reset password</h1>
<p class="row">
<p class="col-md-6 col-md-offset-3">
<%= form_for(@user, url: password_reset_path(params[:id])) do |f| %>
<%= render 'shared/error_messages' %>
<%= hidden_field_tag :email, @user.email %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit "Update password", class: "btn btn-primary" %>
<% end %>
</p>
</p>
app/controllers/password_resets_controller.rb
class PasswordResetsController < ApplicationController
before_action :get_user, only: [:edit, :update]
before_action :valid_user, only: [:edit, :update]
before_action :check_expiration, only: [:edit, :update] # Case (1)
def new
end
def create
@user = User.find_by(email: params[:password_reset][:email].downcase)
if @user
@user.create_reset_digest
@user.send_password_reset_email
flash[:info] = "Email sent with password reset instructions"
redirect_to root_url
else
flash.now[:danger] = "Email address not found"
render 'new'
end
end
def edit
end
def update
if params[:user][:password].empty? # Case (3)
@user.errors.add(:password, "can't be empty")
render 'edit'
elsif @user.update_attributes(user_params) # Case (4)
log_in @user
flash[:success] = "Password has been reset."
redirect_to @user
else
render 'edit' # Case (2)
end
end
private
def user_params
params.require(:user).permit(:password, :password_confirmation)
end
# Before filters
def get_user
@user = User.find_by(email: params[:email])
end
# Confirms a valid user.
def valid_user
unless (@user && @user.activated? &&
@user.authenticated?(:reset, params[:id]))
redirect_to root_url
end
end
# Checks expiration of reset token.
def check_expiration
if @user.password_reset_expired?
flash[:danger] = "Password reset has expired."
redirect_to new_password_reset_url
end
end
end
test/integration/password_resets_test.rb
require 'test_helper'
class PasswordResetsTest < ActionDispatch::IntegrationTest
def setup
ActionMailer::Base.deliveries.clear
@user = users(:michael)
end
test "password resets" do
get new_password_reset_path
assert_template 'password_resets/new'
# 電子郵箱地址無(wú)效
post password_resets_path, params: { password_reset: { email: "" } }
assert_not flash.empty?
assert_template 'password_resets/new'
# 電子郵箱地址有效
post password_resets_path,
params: { password_reset: { email: @user.email } }
assert_not_equal @user.reset_digest, @user.reload.reset_digest
assert_equal 1, ActionMailer::Base.deliveries.size
assert_not flash.empty?
assert_redirected_to root_url
# 密碼重置表單
user = assigns(:user)
# 電子郵箱地址錯(cuò)誤
get edit_password_reset_path(user.reset_token, email: "")
assert_redirected_to root_url
# 用戶未激活
user.toggle!(:activated)
get edit_password_reset_path(user.reset_token, email: user.email)
assert_redirected_to root_url
user.toggle!(:activated)
# 電子郵箱地址正確,令牌不對(duì)
get edit_password_reset_path('wrong token', email: user.email)
assert_redirected_to root_url
# 電子郵箱地址正確,令牌也對(duì)
get edit_password_reset_path(user.reset_token, email: user.email)
assert_template 'password_resets/edit'
assert_select "input[name=email][type=hidden][value=?]", user.email
# 密碼和密碼確認(rèn)不匹配
patch password_reset_path(user.reset_token),
params: { email: user.email,
user: { password: "foobaz",
password_confirmation: "barquux" } }
assert_select 'p#error_explanation'
# 密碼為空值
patch password_reset_path(user.reset_token),
params: { email: user.email,
user: { password: "",
password_confirmation: "" } }
assert_select 'p#error_explanation'
# 密碼和密碼確認(rèn)有效
patch password_reset_path(user.reset_token),
params: { email: user.email,
user: { password: "foobaz",
password_confirmation: "foobaz" } }
assert is_logged_in?
assert_not flash.empty?
assert_redirected_to user
end
end
終端報(bào)錯(cuò)誤:
cmwap:sample_app terry$ rails test
Running via Spring preloader in process 23814
Started with run options --seed 63903
FAIL["test_password_resets", PasswordResetsTest, 1.365597692999927]
test_password_resets#PasswordResetsTest (1.37s)
Expected at least 1 element matching "p#error_explanation", found 0..
Expected 0 to be >= 1.
test/integration/password_resets_test.rb:53:in `block in <class:PasswordResetsTest>'
39/39: [=================================] 100% Time: 00:00:01, Time: 00:00:01
Finished in 1.88071s
39 tests, 186 assertions, 1 failures, 0 errors, 0 skips
擁有18年軟件開發(fā)和IT教學(xué)經(jīng)驗(yàn)。曾任多家上市公司技術(shù)總監(jiān)、架構(gòu)師、項(xiàng)目經(jīng)理、高級(jí)軟件工程師等職務(wù)。 網(wǎng)絡(luò)人氣名人講師,...