Rails 4.1 Mailer Previews and Devise custom emails Rails 4.1 Mailer Previews and Devise custom emails ruby ruby

Rails 4.1 Mailer Previews and Devise custom emails


For those looking to preview Devise emails without using custom mailers, (but still custom emails) this is what I did:

  1. Configure your app for email previewing.

  2. Set up the Devise Mailer Preview class

    a. Rails ~> 4.1

    # mailer/previews/devise_mailer_preview.rbclass Devise::MailerPreview < ActionMailer::Preview  def confirmation_instructions    Devise::Mailer.confirmation_instructions(User.first, "faketoken")  end  def reset_password_instructions    Devise::Mailer.reset_password_instructions(User.first, "faketoken")  end  ...end

    b. Rails ~> 5.0

    class DeviseMailerPreview < ActionMailer::Preview  ... # same setup as Rails 4 above
  3. Restart the server


Using Rails 5 and found the syntax slightly different from @steel's excellent answer, with the use of double "::" being the difference:

# Preview all emails at http://localhost:3000/rails/mailers/devise_mailerclass DeviseMailerPreview < ActionMailer::Preview    def reset_password_instructions        Devise::Mailer.reset_password_instructions(User.first, "faketoken")    endend


You will get undefined method for the devise url helpers when you have forgotten to include the necessary devise modules in your model. e.g. if your model is User and you want the confirmation_url, you must ensure that the :confirmable module is included:

devise <...snipped...>, :confirmable

Be aware that if this module is not currently loaded that your application most likely does not use e.g. confirmations!

Including the :confirmable module might then lock out all your users. See https://github.com/plataformatec/devise/wiki/How-To:-Add-:confirmable-to-Users