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:
Configure your app for email previewing.
Set up the Devise Mailer Preview class
a. Rails ~> 4.1
# mailer/previews/devise_mailer_preview.rb class 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
Restart the server
I found this question because I was trying to figure out how to preview Devise emails myself. I copied your code almost exactly and it works fine for me.
The only thing I did differently was to remove the line layout "notifications_mailer"
from UserMailer
- when I included it I got an error message Missing template layouts/notifications_mailer
. What happens if you remove it?
The line Devise::Controllers::UrlHelpers
definitely includes the method confirmation_url
(you can see this for yourself by opening up the Rails console and running include Devise::Controllers::UrlHelpers
then confirmation_url
, so I suspect the problem is that your previews aren't being rendered by UserMailer
at all. I'm not sure why, but the line layout "notifications_mailer"
might be the culprit.
Do you have a class called NotificationsMailer
? Does including Devise::Controllers::UrlHelpers
in there solve your problem?
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
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_mailer
class DeviseMailerPreview < ActionMailer::Preview
def reset_password_instructions
Devise::Mailer.reset_password_instructions(User.first, "faketoken")
end
end