Conclusion

歷經一番搏鬥,現在網站可以讓使用者註冊、登入、登出、瀏覽個人資料、編輯個人資料、看到所有使用者的列表,還有某些使用者可以具備刪除其他使用者的功能。

先來合併一下:

$ git add -A
$ git commit -m "Finish user edit, update, index, and destroy actions"
$ git checkout master
$ git merge updating-users
$ git push

然後部署,production 環境要重設資料庫的話使用 pg:reset 指令:

$ bundle exec rake test
$ git push heroku
$ heroku pg:reset DATABASE
$ heroku run rake db:migrate
$ heroku run rake db:seed
$ heroku restart

在真實的網站中,你或許不想在資料庫裡增加 sample data(rake db:seed),作者會加入是因為要檢查在 production 環境中的結果。在 production 環境中的使用者列表順序會跟 development 環境的順序不一樣,因為我們沒有指定從資料庫中取回使用者的順序,所以目前的順序由資料庫決定。這對使用者來說沒什麼問題,不過如果是微網誌就不一樣了,之後會解決這個問題。

學到什麼?

  • 使用者可以透過編輯表單更新自己的資料,表單提交後,會向 update action 發出 PATCH 請求
  • 透過使用 strong parameters 可以在網路上安全的更新資料
  • Before filter 提供一種標準的方式,讓特定的 controller action 執行之前,調用方法
  • 透過使用 Before filter 可以實現權限機制
  • 權限機制的測試使用了低階指令,直接向 cintroller actions 送出特定的 HTTP 請求,也使用了高階的整合測試
  • 在使用者登入後,實現友善的導向,讓使用者可以直接瀏覽想去的目的網頁
  • 使用者列表頁面顯示出所有使用者,並且一頁只顯示部分使用者
  • Rails 使用標準文件 db/seeds.rb,讓你可以在資料庫裡增加樣本資料,並且用 rake db:seed 執行
  • 執行 render @users 會自動呼叫 _user.html.erb partial,渲染集合中的每個使用者(on each user in the collection)
  • 在 User model 中增加 admin boolean 屬性後,會自動產生 admin? boolean 方法
  • 管理員(admins)可以透過「delete」連結刪除使用者,「delete」會向 destroy action 發送 DELETE 請求
  • 我們可以透過使用 ERb,在 fixtures 文件裡建立大量的測試使用者