Reputation: 526
I have Devise set up with :validatable on my simple app, but I can't seem to get the registration sign-up form to stop if it caught an error.
User.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
...
end
As of now, I have a sign-up form with standard name/email/password/age fields, and everything works fine when I submit the form that doesn't fail.
But, if I try to create a new user that I know would fail (ex. create a user with an e-mail that already exists), it will still try to create the user, and instead of redirecting back to the sign-up form, it will continue to load the next page as if it was a valid user! Of course, at this point, I get an error because it tries to load up a page with 'current_user' code, and the user doesn't exist.
Where do I even begin to track this down? Not sure if it's the way I installed Devise, or something else?? Please help. Thanks!
Upvotes: 2
Views: 2415
Reputation: 3716
You can use Devise's email validation in non-devise models:
class UserRegistration < ApplicationRecord
validates_format_of :email, with: Devise.email_regexp
end
Upvotes: 0
Reputation: 534
About format of email, I recommend you use devise.rb. There you can set a regex to email, no need put in model.
config.email_regexp = /\A[^@]+@[^@]+\z/
Upvotes: 2
Reputation: 4443
Haven't used validatable, but you could always just roll w/ some of your own validations:
validates :email, uniqueness: true
validates :email, presence: true
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, message: "valid email please"
Upvotes: 2