Rachel9494
Rachel9494

Reputation: 824

Ruby on Rails Devise Reset Password Issue

I am using the letter_opener gem to test my Devise reset password functionality in the development environment.

One of the things I require when a new user signs up is for them to type in their e-mail twice. Here's the code in my User model:

validates :email_confirmation, presence: true
validates_confirmation_of :email

This works fine until I try testing the password reset functionality. On the page you input your new password twice and submit, it throws an error that Email confirmation can't be blank. Even if I stick the e-mail and e-mail confirmation fields in there to see what happens and type something in it, it still thinks the field is blank.

I could change my model code to look something like:

validates :email_confirmation, presence: true, unless: {action: :edit}
validates_confirmation_of :email, unless: {action: :edit}

But then it won't keep the e-mail confirmation validation if a user is just regularly editing his/her e-mail address in their account profile.

So, I would like to know if there is a way to either make it so that the reset password page doesn't think it needs the e-mail confirmation, or if there is a way to edit my unless statement in my model to apply the exception only when specifically editing on the password reset page. Thanks for any help, on Rails 4 with Devise 3.2.4.

Upvotes: 0

Views: 435

Answers (1)

backpackerhh
backpackerhh

Reputation: 2353

Have you tried this?:

validates :email_confirmation, presence: true, if: :new_record?

Edit

If you want to validate this field only if email has changed, you could do this:

validates :email_confirmation, presence: true, if: :email_changed?

I think it would be valid to both cases:

  • when user is a new record
  • when user edits the email

Try this out!

Upvotes: 1

Related Questions