<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之旅 廣告
                # 6.5 練習 電子書中有練習的答案,如果想閱讀參考答案,請[購買電子書](http://railstutorial-china.org/#purchase)。 避免練習和正文沖突的方法參見[3.6 節](chapter3.html#mostly-static-pages-exercises)中的說明。 1. 為[代碼清單 6.31](#listing-email-downcase) 中把電子郵件地址轉換成小寫字母形式的代碼編寫一個測試,如[代碼清單 6.41](#listing-email-downcase-test) 所示。這段測試使用 `reload` 方法從數據庫中重新加載數據;使用 `assert_equal` 方法測試是否相等。為了驗證[代碼清單 6.41](#listing-email-downcase-test) 是正確的,先把 `before_save` 那行注釋掉,看測試是否失敗,然后去掉注釋,看測試能否通過。 2. 在 `before_save` 回調中使用 `email.downcase!` 直接修改 `email` 屬性的值([代碼清單 6.42](#listing-downcase-bang)),運行測試組件,確認可以這么做。 3. 我們在 [6.2.4 節](#format-validation)說過,[代碼清單 6.21](#listing-validates-format-of-email) 中的電子郵件地址正則表達式能匹配出現連續點號的無效地址,例如 _foo@bar..com_。把這個地址添加到[代碼清單 6.19](#listing-email-format-validation-tests) 中的無效地址列表中,讓測試失敗,然后使用[代碼清單 6.43](#listing-better-email-regex) 中較復雜的正則表達式讓測試通過。 ##### 代碼清單 6.41:[代碼清單 6.31](#listing-email-downcase) 中把電子郵件地址轉換成小寫形式的測試 test/models/user_test.rb ``` require 'test_helper' class UserTest < ActiveSupport::TestCase def setup @user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar") end . . . test "email addresses should be unique" do duplicate_user = @user.dup duplicate_user.email = @user.email.upcase @user.save assert_not duplicate_user.valid? end test "email addresses should be saved as lower-case" do mixed_case_email = "Foo@ExAMPle.CoM" @user.email = mixed_case_email @user.save assert_equal mixed_case_email.downcase, @user.reload.email end test "password should have a minimum length" do @user.password = @user.password_confirmation = "a" * 5 assert_not @user.valid? end end ``` ##### 代碼清單 6.42:`before_save` 回調的另一種實現方式 GREEN app/models/user.rb ``` class User < ActiveRecord::Base before_save { email.downcase! } validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } has_secure_password validates :password, presence: true, length: { minimum: 6 } end ``` ##### 代碼清單 6.43:不允許電子郵件地址中有多個點號的正則表達式 GREEN app/models/user.rb ``` class User < ActiveRecord::Base before_save { email.downcase! } validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } has_secure_password validates :password, presence: true, length: { minimum: 6 } 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>

                              哎呀哎呀视频在线观看