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?

Tom
  • 15,527
  • 5
  • 48
  • 62
Evan Machnic
  • 637
  • 1
  • 6
  • 8

6 Answers6

83

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

Community
  • 1
  • 1
Tilo
  • 33,354
  • 5
  • 79
  • 106
  • This should be marked as the answer, as well as adding a bit from "http://stackoverflow.com/a/7577878/602588" (answer below). – jackyalcine Jul 23 '12 at 20:56
  • 2
    You will also need to add the line: add_column :users, :unconfirmed_email, :string – Calciphus Sep 25 '12 at 01:06
  • is that a recent change in Devise? – Tilo Sep 26 '12 at 01:42
  • 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. – Kyle Carlson May 26 '13 at 19:14
  • 2
    Also check this: [How To: Add :confirmable to Users](https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users) – Alexander Popov Sep 30 '13 at 11:19
  • 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 reconfirmable` – JTE Aug 05 '20 at 10:01
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

Piotr Mąsior
  • 1,580
  • 1
  • 11
  • 20
  • 4
    It should be noted that the style of the migration is now out of date as per V2. https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0-migration-schema-style – tibbon Apr 18 '12 at 00:20
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  }
Jeremy Thomas
  • 479
  • 5
  • 10
  • don't need to do a migration as Tilo suggests? – Leahcim Jan 28 '12 at 01:41
  • 3
    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.erb` – jackyalcine Jul 23 '12 at 21:03
  • Hi, what is correct setting for :domain? is it domain of my heroku app or? – Skodik.o Jan 10 '16 at 11:01
11

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

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
cintrzyk
  • 447
  • 3
  • 5
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  }
Michael Wong
  • 115
  • 1
  • 2
  • 7
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]

Community
  • 1
  • 1
Moon Man
  • 1
  • 1