# 10.5 練習
電子書中有練習的答案,如果想閱讀參考答案,請[購買電子書](http://railstutorial-china.org/#purchase)。
避免練習和正文沖突的方法參見[3.6 節](chapter3.html#mostly-static-pages-exercises)中的說明。
1. 填寫[代碼清單 10.57](#listing-password-reset-expire-test) 中缺少的代碼,為[代碼清單 10.52](#listing-password-reset-update-action) 中的密碼重設超時失效分支編寫集成測試。([代碼清單 10.57](#listing-password-reset-expire-test) 用到了 `response.body`,用來獲取返回頁面中的 HTML。)檢查是否過期有很多方法,[代碼清單 10.57](#listing-password-reset-expire-test) 使用的方法是,檢查響應主體中是否包含單詞“expired”(不區分大小寫)。
2. 現在,用戶列表頁面會顯示所有用戶,而且各用戶還可以通過 /users/:id 查看。不過,更合理的做法是只顯示已激活的用戶。填寫[代碼清單 10.58](#listing-show-only-active-users-exercise) 中缺少的代碼,實現這一需求。[[9](#fn-9)](這段代碼中使用了 Active Record 提供的 `where` 方法,[11.3.3 節](chapter11.html#a-proto-feed)會詳細介紹。)附加題:為 /users 和 /users/:id 編寫集成測試。
3. 在[代碼清單 10.42](#listing-user-model-password-reset) 中,`activate` 和 `create_reset_digest` 方法中都調用了兩次 `update_attribute` 方法,每一次調用都要單獨執行一個數據庫事務(transaction)。填寫[代碼清單 10.59](#listing-update-columns) 中缺少的代碼,把兩個 `update_attribute` 調用換成一個 `update_columns` 調用,這樣修改后每個方法只會和數據庫交互一次。然后再運行測試組件,確保仍能通過。
##### 代碼清單 10.57:測試密碼重設超時失效了 GREEN
test/integration/password_resets_test.rb
```
require 'test_helper'
class PasswordResetsTest < ActionDispatch::IntegrationTest
def setup
ActionMailer::Base.deliveries.clear
@user = users(:michael)
end
.
.
.
test "expired token" do
get new_password_reset_path
post password_resets_path, password_reset: { email: @user.email }
@user = assigns(:user)
@user.update_attribute(:reset_sent_at, 3.hours.ago)
patch password_reset_path(@user.reset_token),
email: @user.email,
user: { password: "foobar",
password_confirmation: "foobar" }
assert_response :redirect
follow_redirect!
assert_match /FILL_IN/i, response.body end
end
```
##### 代碼清單 10.58:只顯示已激活用戶的代碼模板
app/controllers/users_controller.rb
```
class UsersController < ApplicationController
.
.
.
def index
@users = User.where(activated: FILL_IN).paginate(page: params[:page]) end
def show
@user = User.find(params[:id])
redirect_to root_url and return unless FILL_IN end
.
.
.
end
```
##### 代碼清單 10.59:使用 `update_columns` 的代碼模板
app/models/user.rb
```
class User < ActiveRecord::Base
attr_accessor :remember_token, :activation_token, :reset_token
before_save :downcase_email
before_create :create_activation_digest
.
.
.
# 激活賬戶
def activate
update_columns(activated: FILL_IN, activated_at: FILL_IN) end
# 發送激活郵件
def send_activation_email
UserMailer.account_activation(self).deliver_now
end
# 設置密碼重設相關的屬性
def create_reset_digest
self.reset_token = User.new_token
update_columns(reset_digest: FILL_IN, reset_sent_at: FILL_IN) end
# 發送密碼重設郵件
def send_password_reset_email
UserMailer.password_reset(self).deliver_now
end
.
.
.
end
```
- 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 練習