[ACCEPTED]-Rails 4.1 Mailer Previews and Devise custom emails-devise

Accepted answer
Score: 60

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

  1. Configure 3 your app for email previewing.

  2. Set up the 2 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 1 ~> 5.0

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

Score: 7

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

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

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

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

Score: 7

Using Rails 5 and found the syntax slightly 2 different from @steel's excellent answer, with 1 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
Score: 5

I found this question because I was trying 14 to figure out how to preview Devise emails 13 myself. I copied your code almost exactly 12 and it works fine for me.

The only thing 11 I did differently was to remove the line 10 layout "notifications_mailer" from UserMailer - when I included it I got an error 9 message Missing template layouts/notifications_mailer. What happens if you remove it?

The 8 line Devise::Controllers::UrlHelpers definitely includes the method confirmation_url (you 7 can see this for yourself by opening up 6 the Rails console and running include Devise::Controllers::UrlHelpers then confirmation_url, so 5 I suspect the problem is that your previews 4 aren't being rendered by UserMailer at all. I'm not 3 sure why, but the line layout "notifications_mailer" might be the culprit.

Do 2 you have a class called NotificationsMailer? Does including 1 Devise::Controllers::UrlHelpers in there solve your problem?

More Related questions