0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

rails tutorial 8章リスト8.28がREDから変わらない人に向けて

Posted at

同じ所で躓いてる同士に向けて。
rails tutorial 8章リスト8.28がGREENにならず、以下ログが出力される。

"foo@bar..com" should be invalid

対応内容

/app/models/user.rb
正規表現を修正

class User < ApplicationRecord
  before_save { self.email = email.downcase }
  validates :name,  presence: true, length: { maximum: 50 }
-  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
+  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 }

原因

メールアドレスの正規表現
上記様を参考に正規表現を読み解くと

正規表現 意味
[a-z\d-.]+ 英小文字、数字、ハイフン、ドットのいずれかを少なくとも1文字以上繰り返す
. ドット

これだと「..」でもOKとなってしまう。
そのため
「英小文字、数字、ハイフン、ドットのいずれかを少なくとも1文字以上繰り返す」ではなく、
「英小文字、数字、ハイフンのいずれかを少なくとも1文字以上繰り返す」に修正。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?