Rails 4.1 Mailer Previews and Devise custom emails

后端 未结 4 1227
星月不相逢
星月不相逢 2020-12-29 19:43

I have a brand new Rails 4.1.1 app where I\'m customizing the Devise emails. I want to have them displayed on the new Rails email preview feature so I did the following:

相关标签:
4条回答
  • 2020-12-29 19:57

    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
    
    0 讨论(0)
  • 2020-12-29 19:58

    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?

    0 讨论(0)
  • 2020-12-29 20:04

    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

    0 讨论(0)
  • 2020-12-29 20:12

    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.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
      
    3. Restart the server

    0 讨论(0)
提交回复
热议问题