# 9.6 練習
電子書中有練習的答案,如果想閱讀參考答案,請[購買電子書](http://railstutorial-china.org/#purchase)。
避免練習和正文沖突的方法參見[3.6 節](chapter3.html#mostly-static-pages-exercises)中的說明。
1. 編寫一個測試,確保友好轉向只會在首次登錄后轉向指定的地址,以后再登錄都會轉向默認地址(即資料頁面)。提示:把這個測試添加到[代碼清單 9.26](#listing-friendly-forwarding-test) 中,檢查 `session[:forwarding_url]` 中是否保存了正確的值。
2. 編寫一個集成測試,測試布局中的所有鏈接,以及登錄后和登錄前應該看到哪些鏈接。提示:把這個測試添加到[代碼清單 5.25](chapter5.html#listing-layout-links-test) 中,使用 `log_in_as` 輔助方法。
3. 參照[代碼清單 9.59](#listing-forbidden-admin-test),直接向 `update` 動作發送 `PATCH` 請求,確認無法修改 `admin` 屬性。為了確保測試寫得正確,首先應該把 `admin` 添加到允許修改的參數列表 `user_params` 中,所以在此之前測試組件無法通過。
4. 使用[代碼清單 9.60](#listing-new-edit-partial) 中的局部視圖重構 `new.html.erb` 和 `edit.html.erb` 視圖中的表單,重構后的代碼如[代碼清單 9.61](#listing-new-user-with-partial) 和[代碼清單 9.62](#listing-edit-user-with-partial) 所示。注意這里使用 `provide` 方法([3.4.3 節](chapter3.html#layouts-and-embedded-ruby)用過)避免布局中有重復。[[12](#fn-12)]
##### 代碼清單 9.59:測試禁止修改 `admin` 屬性
test/controllers/users_controller_test.rb
```
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
def setup
@user = users(:michael)
@other_user = users(:archer)
end
.
.
.
test "should redirect update when logged in as wrong user" do
log_in_as(@other_user)
patch :update, id: @user, user: { name: @user.name, email: @user.email }
assert_redirected_to root_url
end
test "should not allow the admin attribute to be edited via the web" do log_in_as(@other_user) assert_not @other_user.admin? patch :update, id: @other_user, user: { password: FILL_IN, password_confirmation: FILL_IN, admin: FILL_IN } assert_not @other_user.FILL_IN.admin? end .
.
.
end
```
##### 代碼清單 9.60:`new` 和 `edit` 視圖中使用的表單局部視圖
app/views/users/_form.html.erb
```
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages', object: @user %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.email_field :email, class: 'form-control' %>
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
<%= f.submit yield(:button_text), class: "btn btn-primary" %>
<% end %>
```
##### 代碼清單 9.61:使用局部視圖的注冊頁面視圖
app/views/users/new.html.erb
```
<% provide(:title, 'Sign up') %>
<% provide(:button_text, 'Create my account') %>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= render 'form' %>
</div>
</div>
```
##### 代碼清單 9.62:使用局部視圖的編輯頁面視圖
app/views/users/edit.html.erb
```
<% provide(:title, 'Edit user') %>
<% provide(:button_text, 'Save changes') %>
<h1>Update your profile</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= render 'form' %>
<div class="gravatar_edit">
<%= gravatar_for @user %>
<a href="http://gravatar.com/emails" target="_blank">Change</a>
</div>
</div>
</div>
```
- 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 練習