目的
デフォルト状態では、
新規登録時(confirmation)とメールアドレス変更時(reconfirmation)に送信するメールのテンプレートが共通で、細かく分けたい時に不便である。例えばメール本文(body)は条件分岐などで対処できるが、件名(subject)は変更できない。
deviseではテンプレートを分けるための拡張ポイントDevise::Models::Confirmable#send_on_create_confirmation_instructionsを用意している。
deviseを継承したモデルで拡張できるので、簡単に別のテンプレートを定義することができる。
今回のサンプルはこちらに置いています。
手順
Deviseのインストール
$ rails new devise-sample -T --skip-bundle
+gem 'devise'
+group :development do
+  gem 'letter_opener_web' # これは無くてもOK
+end
$ bundle install
$ rails generate devise:install
Rails.application.configure do
# ...
  config.action_mailer.raise_delivery_errors = false
+  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
+  config.action_mailer.delivery_method = :letter_opener_web
# ...
$ rails generate devise user
$ rails g devise:views
confirmable, reconfirmableを有効にする
class User < ActiveRecord::Base
   devise :database_authenticatable, :registerable,
-         :recoverable, :rememberable, :trackable, :validatable
+         :recoverable, :rememberable, :trackable, :validatable,
+         :confirmable
 end
class DeviseCreateUsers < ActiveRecord::Migration
       t.string   :last_sign_in_ip
       ## Confirmable
-      # t.string   :confirmation_token
-      # t.datetime :confirmed_at
-      # t.datetime :confirmation_sent_at
-      # t.string   :unconfirmed_email # Only if using reconfirmable
+      t.string   :confirmation_token
+      t.datetime :confirmed_at
+      t.datetime :confirmation_sent_at
+      t.string   :unconfirmed_email # Only if using reconfirmable
end
+  config.reconfirmable = true
テンプレートを分ける
| 用途 | テンプレート | |
|---|---|---|
| 新規登録(confirmation) | confirmation_on_create_instructions.html.erb | 任意の名前(統一する) | 
| メールアドレス変更(reconfirmation) | confirmation_instructions.html.erb | デフォルト | 
Devise::Models::Confirmable::send_confirmation_instructionsを参考に実装しました。
class User < ActiveRecord::Base
   devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :trackable, :validatable,
          :confirmable
+
+  # override Devise::Models::Confirmable#send_on_create_confirmation_instructions
+  def send_on_create_confirmation_instructions
+    generate_confirmation_token!  unless @raw_confirmation_token
+    send_devise_notification(:confirmation_on_create_instructions, @raw_confirmation_token, {})
+  end
 end
-<p>Welcome <%= @email %>!</p>
+<p>Reconfirmation <%= @email %>!</p>
 
 <p>You can confirm your account email through the link below:</p>
+<p>Welcome <%= @email %>!</p>
+
+<p>You can confirm your account email through the link below:</p>
+
+<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
Devise::Mailerを参考に実装しました。
+if defined?(ActionMailer)
+  class Devise::Mailer < Devise.parent_mailer.constantize
+    include Devise::Mailers::Helpers
+
+    def confirmation_instructions(record, token, opts={})
+      @token = token
+      devise_mail(record, :confirmation_instructions, opts)
+    end
+
+    def reset_password_instructions(record, token, opts={})
+      @token = token
+      devise_mail(record, :reset_password_instructions, opts)
+    end
+
+    def unlock_instructions(record, token, opts={})
+      @token = token
+      devise_mail(record, :unlock_instructions, opts)
+    end
+
+    # 新規追加!
+    def confirmation_on_create_instructions(record, token, opts={})
+      @token = token
+      devise_mail(record, :confirmation_on_create_instructions, opts)
+    end
+  end
+end
       unauthenticated: "You need to sign in or sign up before continuing."
       unconfirmed: "You have to confirm your email address before continuing."
     mailer:
-      confirmation_instructions:
+      confirmation_on_create_instructions:
         subject: "Confirmation instructions"
+      confirmation_instructions:
+        subject: "Reconfirmation instructions"
       reset_password_instructions:
         subject: "Reset password instructions"
       unlock_instructions:
確認
sign_upしてメールが飛んでるか確認する(下記では会員登録時だけ..)
letter_opener_web便利
参考
http://willnet.in/86
https://github.com/plataformatec/devise
https://github.com/fgrehm/letter_opener_web

