Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails & Devise: Email Confirmation text field?

I am using Rails 3.2 and Devise. I am wondering if Devise provides an "Email (address) Confirmation" field just like Password Confirmation for the user to type in a Password field and then type in a Password Confirmation field before the website sends out a confirmation email or processes the sign up registration. If not, must I add a email_confirmation and validate the User model (after rails g devise User)?

Thanks in advance

like image 405
smileymike Avatar asked Aug 13 '12 21:08

smileymike


Video Answer


1 Answers

I haven't found if devise does what you ask (the question is referring to a second :email_confirmation field similar to :password_confirmation, 'confirmable' seems to be solely related to confirmation links). I had to do this as well and I figured out how to do it using builtin active record validation (rails 3.2.8, devise 2.1.2):

  1. In your model add confirmation: true to your email validation and :email_confirmation to attr_accessible.
  2. In your view add an :email_confirmation field.

As an example, in your model (not assuming other validations or attr_accessibles):

attr_accessible :email, :email_confirmation
validates :email, confirmation: true

which will use the builtin active record 'confirmation' validation (the same as for passwords). Finally, in your view(s) you'll need to add something like:

<div><%= f.label :email_confirmation %>
<%= f.email_field :email_confirmation %></div>

There may be a cleaner way but this worked for me. However, my only experience with devise so far has been when adding it to existing models, I haven't used it when it's generating the models for you (presumably it's mostly the same).

like image 147
nighliber Avatar answered Nov 04 '22 08:11

nighliber