<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 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 id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看