How do I enable :confirmable in Devise?
Asked Answered
C

6

47

The newest version of Devise doesn't have :confirmable enabled by default. I already added the respective columns to the User model but cannot find any code examples of how to enable :confirmable.

Where can I find a good example or what code do I need to enable it?

Chingchinghai answered 24/1, 2011 at 14:52 Comment(0)
R
85

to "enable" confirmable, you just need to add it to your model, e.g.:

class User
  # ...
  devise :confirmable , ....
  # ...
end

after that, you'll have to create and run a migration which adds the required columns to your model:

# rails g migration add_confirmable_to_devise
class AddConfirmableToDevise < ActiveRecord::Migration
  def self.up
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at,       :datetime
    add_column :users, :confirmation_sent_at , :datetime
    add_column :users, :unconfirmed_email, :string

    add_index  :users, :confirmation_token, :unique => true
  end
  def self.down
    remove_index  :users, :confirmation_token

    remove_column :users, :unconfirmed_email
    remove_column :users, :confirmation_sent_at
    remove_column :users, :confirmed_at
    remove_column :users, :confirmation_token
  end
end

see: Adding confirmable module to an existing site using Devise

I'd recommend to check the source code to see how Confirmable works:

https://github.com/plataformatec/devise/blob/master/lib/devise/models/confirmable.rb

You could also check the RailsCast on Devise:

http://railscasts.com/episodes/209-introducing-devise

Next it would be best to search for example applications on GitHub

Reckford answered 4/10, 2011 at 21:49 Comment(6)
This should be marked as the answer, as well as adding a bit from "https://mcmap.net/q/360086/-how-do-i-enable-confirmable-in-devise" (answer below).Shutz
You will also need to add the line: add_column :users, :unconfirmed_email, :stringFluter
is that a recent change in Devise?Reckford
Thanks for that! Helped me enable it in my app in about 2 minutes. One tip: if you enable it after you already have registered users, the confirmation probably won't work for them (it didn't for me). I recreated the user, confirmed the account, and everything worked perfectly.Kanzu
Also check this: How To: Add :confirmable to UsersCroquette
From the recent gist on the devise github page, it does not look like unconfirmed_email is needed unless doing reconfirmable: # add_column :users, :unconfirmed_email, :string # Only if using reconfirmableKim
O
19

This question seems to be odd one ;-) If you written some migration alike:

    change_table(:users) do |t|
      t.confirmable
    end
    add_index :users, :confirmation_token,   :unique => true

and as you said little change in model (passing additional => :confirmable to devise) like so:

    devise :database_authenticatable, :registerable, :confirmable

you can now generate some views (if you didn')

    rails generate devise:views

You can go to app/views/devise/confirmations/new.html.erb and check how it looks like or change it. Furthermore you can inspect app/views/devise/confirmations/shared/_links.erb => there is line:

    <%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>

This condition checks if confirmable is turned on so... technically if everything went fine it should works OOTB. After creating new account - in log - you should see lines where confirmation mail is sent with appropriate link. It triggers:

     Rendered devise/mailer/confirmation_instructions.html.erb

so you have got next place where you can customize it a bit

How to customize confirmation strategy? Please ask exact question what do you want to achieve. You can check devise gem path. In /lib/devise/models/confirmable.rb some comments could be helpful.

regards

Ozonolysis answered 25/1, 2011 at 18:18 Comment(1)
It should be noted that the style of the migration is now out of date as per V2. github.com/plataformatec/devise/wiki/…Statistician
S
18

If you've already installed devise into your app, and want to add "confirmable" later, instead of running:

rails generate devise:views

as mentioned by Piotr, run

rails generate devise:views confirmable

to produce only the views needed for "confirmable". You'll see output like this:

rails generate devise:views confirmable
    invoke  Devise::Generators::SharedViewsGenerator
    create    app/views/confirmable/mailer
    create    app/views/confirmable/mailer/confirmation_instructions.html.erb
    create    app/views/confirmable/mailer/reset_password_instructions.html.erb
    create    app/views/confirmable/mailer/unlock_instructions.html.erb
    create    app/views/confirmable/shared
    create    app/views/confirmable/shared/_links.erb
    invoke  form_for
    create    app/views/confirmable/confirmations
    create    app/views/confirmable/confirmations/new.html.erb
    create    app/views/confirmable/passwords
    create    app/views/confirmable/passwords/edit.html.erb
    create    app/views/confirmable/passwords/new.html.erb
    create    app/views/confirmable/registrations
    create    app/views/confirmable/registrations/edit.html.erb
    create    app/views/confirmable/registrations/new.html.erb
    create    app/views/confirmable/sessions
    create    app/views/confirmable/sessions/new.html.erb
    create    app/views/confirmable/unlocks
    create    app/views/confirmable/unlocks/new.html.erb 

You'll then be able to access these files directly in your project to style them like your application. You'll also be able to change the messaging in the emails Devise sends out through the generated mailer views.

Last, don't forget to add config.action_mailer.delivery_method and config.action_mailer.smtp_settings in your app/config/environments/{environment_name}.rb file. This is what my production.rb file looks like:

  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :domain               => '[redacted]',
    :user_name            => '[redacted]',
    :password             => '[redacted]',
    :authentication       => 'plain',
    :enable_starttls_auto => true  }
Swanskin answered 28/9, 2011 at 3:9 Comment(3)
don't need to do a migration as Tilo suggests?Caressive
Er, this is a bit off. All of the views created here won't help. You only really need: create app/views/confirmable/confirmations create app/views/confirmable/confirmations/new.html.erb create app/views/confirmable/mailer/confirmation_instructions.html.erbShutz
Hi, what is correct setting for :domain? is it domain of my heroku app or?Schmooze
R
11

Checkout devise wiki page. There is a full answer for your question.

Ratfink answered 10/12, 2012 at 20:26 Comment(0)
I
2

For DRY, you can also put mailer config in config/initializers/mail.rb like:

ActionMailer::Base.smtp_settings = {
    :address              => "smtp.gmail.com",
    :port                 => 587,
    :domain               => '[redacted]',
    :user_name            => '[redacted]',
    :password             => '[redacted]',
    :authentication       => 'plain',
    :enable_starttls_auto => true  }
Immoderate answered 6/11, 2011 at 5:16 Comment(0)
Q
0

After configuring the ActionMailer setting described above I had to make one last addition to the config/environments/development.rb file to fix an error page that would show up after registering a new user:

config.action_mailer.default_url_options = { :host => 'localhost' }

More details about this solution: Heroku/devise - Missing host to link to! Please provide :host parameter or set default_url_options[:host]

Quieten answered 27/3, 2015 at 22:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.