# 3.6 練習
從這以后,我建議在單獨的主題分支中做練習:
```
$ git checkout static-pages
$ git checkout -b static-pages-exercises
```
這么做就不會和本書正文產生沖突了。
練習做完后,可以把相應的分支推送到遠程倉庫中(如果有遠程倉庫的話):
```
# 做完第一個練習后
$ git commit -am "Eliminate repetition (solves exercise 3.1)"
# 做完第二個練習后
$ git add -A
$ git commit -m "Add a Contact page (solves exercise 3.2)"
$ git push -u origin static-pages-exercises
$ git checkout master
```
(為了繼續后面的開發,最后一步只切換到主分支,但不合并,以免和正文產生沖突。)在后面的章節中,使用的分支和提交消息有所不同,但基本思想是一致的。
電子書中有練習的答案,如果想閱讀參考答案,請[購買電子書](http://railstutorial-china.org/#purchase)。
1. 你可能注意到了,靜態頁面控制器的測試([代碼清單 3.22](#listing-title-tests))中有些重復,每個標題測試中都有“Ruby on Rails Tutorial Sample App”。我們要使用特殊的函數 `setup` 去除重復。這個函數在每個測試運行之前執行。請你確認[代碼清單 3.38](#listing-base-title-test) 中的測試仍能通過。([代碼清單 3.38](#listing-base-title-test) 中使用了一個實例變量,[2.2.2 節](chapter2.html#mvc-in-action)簡單介紹過,[4.4.5 節](chapter4.html#a-user-class)會進一步介紹。這段代碼中還使用了字符串插值操作,[4.2.2 節](chapter4.html#strings)會做介紹。)
2. 為這個演示應用添加一個“聯系”頁面。[[14](#fn-14)]參照[代碼清單 3.13](#listing-about-test),先編寫一個測試,檢查頁面的標題是否為 “Contact | Ruby on Rails Tutorial Sample App”,從而確定 /static_pages/contact 對應的頁面是否存在。把[代碼清單 3.39](#listing-proposed-contact-page) 中的內容寫入“聯系”頁面的視圖,讓測試通過。注意,這個練習是獨立的,所以[代碼清單 3.39](#listing-proposed-contact-page) 中的代碼不會影響[代碼清單 3.38](#listing-base-title-test) 中的測試。
##### 代碼清單 3.38:使用通用標題的靜態頁面控制器測試 GREEN
test/controllers/static_pages_controller_test.rb
```
require 'test_helper'
class StaticPagesControllerTest < ActionController::TestCase
def setup @base_title = "Ruby on Rails Tutorial Sample App" end
test "should get home" do
get :home
assert_response :success
assert_select "title", "Home | #{@base_title}" end
test "should get help" do
get :help
assert_response :success
assert_select "title", "Help | #{@base_title}" end
test "should get about" do
get :about
assert_response :success
assert_select "title", "About | #{@base_title}" end
end
```
##### 代碼清單 3.39:“聯系”頁面的內容
app/views/static_pages/contact.html.erb
```
<% provide(:title, "Contact") %>
<h1>Contact</h1>
<p>
Contact the Ruby on Rails Tutorial about the sample app at the
<a href="http://www.railstutorial.org/#contact">contact page</a>.
</p>
```
- 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 練習