# 8.3 退出
[8.1 節](#sessions)說過,我們要實現的認證系統會記住用戶的登錄狀態,直到用戶自行退出為止。本節,我們就要實現退出功能。退出鏈接已經定義好了([代碼清單 8.16](#listing-layout-login-logout-links)),所以我們只需編寫一個正確的控制器動作,銷毀用戶會話。
目前為止,會話控制器的動作都遵從了 REST 架構,`new` 動作用于登錄頁面,`create` 動作完成登錄操作。我們要繼續使用 REST 架構,添加一個 `destroy` 動作,刪除會話,實現退出功能。登錄功能在[代碼清單 8.13](#listing-log-in-success) 和[代碼清單 8.22](#listing-login-upon-signup) 中都用到了,但退出功能不同,只在一處使用,所以我們會直接把相關的代碼寫在 `destroy` 動作中。[8.4.6 節](#remember-tests)會看到,這么做(稍微重構后)易于測試認證系統。
退出要撤銷 `log_in`([代碼清單 8.12](#listing-log-in-function))完成的操作,即從會話中刪除用戶的 ID。為此,我們要使用 `delete` 方法,如下所示:
```
session.delete(:user_id)
```
我們還要把當前用戶設為 `nil`。不過在現在這種情況下做不做這一步都沒關系,因為退出后會立即轉向根地址。[[13](#fn-13)]和 `log_in` 及相關的方法一樣,我們要把 `log_out` 方法放在會話輔助方法模塊中,如[代碼清單 8.26](#listing-log-out-method) 所示。
##### 代碼清單 8.26:`log_out` 方法
app/helpers/sessions_helper.rb
```
module SessionsHelper
# 登入指定的用戶
def log_in(user)
session[:user_id] = user.id
end
.
.
.
# 退出當前用戶
def log_out
session.delete(:user_id) @current_user = nil end
end
```
然后,在會話控制器的 `destroy` 動作中調用 `log_out` 方法,如[代碼清單 8.27](#listing-destroy-session) 所示。
##### 代碼清單 8.27:銷毀會話(退出用戶)
app/controllers/sessions_controller.rb
```
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
log_out redirect_to root_url end
end
```
我們可以在[代碼清單 8.20](#listing-user-login-test-valid-information) 中的用戶登錄測試中添加一些步驟,測試退出功能。登錄后,使用 `delete` 方法向退出地址([表 8.1](#table-restful-sessions))發起 `DELETE` 請求,然后確認用戶已經退出,而且重定向到了根地址。我們還要確認出現了登錄鏈接,而且退出和資料頁面的鏈接消失了。測試中新加入的步驟如[代碼清單 8.28](#listing-user-logout-test) 所示。
##### 代碼清單 8.28:測試用戶退出功能 GREEN
test/integration/users_login_test.rb
```
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
.
.
.
test "login with valid information followed by logout" do get login_path
post login_path, session: { email: @user.email, password: 'password' }
assert is_logged_in? assert_redirected_to @user
follow_redirect!
assert_template 'users/show'
assert_select "a[href=?]", login_path, count: 0
assert_select "a[href=?]", logout_path
assert_select "a[href=?]", user_path(@user)
delete logout_path assert_not is_logged_in? assert_redirected_to root_url follow_redirect! assert_select "a[href=?]", login_path assert_select "a[href=?]", logout_path, count: 0 assert_select "a[href=?]", user_path(@user), count: 0 end
end
```
(現在可以在測試中使用 `is_logged_in?` 了,所以向登錄地址發送有效信息之后,我們添加了 `assert is_logged_in?`。)
定義并測試了 `destroy` 動作之后,注冊、登錄和退出三大功能就都實現了。現在測試組件應該可以通過:
##### 代碼清單 8.29:**GREEN**
```
$ bundle exec rake test
```
- Ruby on Rails 教程
- 致中國讀者
- 序
- 致謝
- 作者譯者簡介
- 版權和代碼授權協議
- 第 1 章 從零開始,完成一次部署
- 1.1 簡介
- 1.2 搭建環境
- 1.3 第一個應用
- 1.4 使用 Git 做版本控制
- 1.5 部署
- 1.6 小結
- 1.7 練習
- 第 2 章 玩具應用
- 2.1 規劃應用
- 2.2 用戶資源
- 2.3 微博資源
- 2.4 小結
- 2.5 練習
- 第 3 章 基本靜態的頁面
- 3.1 創建演示應用
- 3.2 靜態頁面
- 3.3 開始測試
- 3.4 有點動態內容的頁面
- 3.5 小結
- 3.6 練習
- 3.7 高級測試技術
- 第 4 章 Rails 背后的 Ruby
- 4.1 導言
- 4.2 字符串和方法
- 4.3 其他數據類型
- 4.4 Ruby 類
- 4.5 小結
- 4.6 練習
- 第 5 章 完善布局
- 5.1 添加一些結構
- 5.2 Sass 和 Asset Pipeline
- 5.3 布局中的鏈接
- 5.4 用戶注冊:第一步
- 5.5 小結
- 5.6 練習
- 第 6 章 用戶模型
- 6.1 用戶模型
- 6.2 用戶數據驗證
- 6.3 添加安全密碼
- 6.4 小結
- 6.5 練習
- 第 7 章 注冊
- 7.1 顯示用戶的信息
- 7.2 注冊表單
- 7.3 注冊失敗
- 7.4 注冊成功
- 7.5 專業部署方案
- 7.6 小結
- 7.7 練習
- 第 8 章 登錄和退出
- 8.1 會話
- 8.2 登錄
- 8.3 退出
- 8.4 記住我
- 8.5 小結
- 8.6 練習
- 第 9 章 更新,顯示和刪除用戶
- 9.1 更新用戶
- 9.2 權限系統
- 9.3 列出所有用戶
- 9.4 刪除用戶
- 9.5 小結
- 9.6 練習
- 第 10 章 賬戶激活和密碼重設
- 10.1 賬戶激活
- 10.2 密碼重設
- 10.3 在生產環境中發送郵件
- 10.4 小結
- 10.5 練習
- 10.6 證明超時失效的比較算式
- 第 11 章 用戶的微博
- 11.1 微博模型
- 11.2 顯示微博
- 11.3 微博相關的操作
- 11.4 微博中的圖片
- 11.5 小結
- 11.6 練習
- 第 12 章 關注用戶
- 12.1 “關系”模型
- 12.2 關注用戶的網頁界面
- 12.3 動態流
- 12.4 小結
- 12.5 練習