Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
メールフィールドを持つRailsアプリケーションにユーザーモデルがあります。電子メールが正しい形式であることを保証するデフォルトの検証はありますか? そうでない場合、そのフィールドを検証するにはどうすればよいですか?
gemfile に追加します。
gem 'validates_email_format_of'
そしてあなたのモデルでは:
validates :email, email_format: { message: "doesn't look like an email address" }
または、gem を使用したくない場合は、正規表現を使用します。
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i