Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4.1 Mailer Previews and Devise custom emails

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:

1) Added the following snippet to my config/development.rb file:

config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews" 

2) Created my custom Devise email UserMailer in app/mailers/user_mailer.rb:

class UserMailer < Devise::Mailer      helper :application # gives access to all helpers defined within `application_helper`.   include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`    layout "notifications_mailer" end 

3) Changed config/initializers/devise.rb to contain the following snippet:

config.mailer = 'UserMailer' 

4) Added the class UserMailerPreview to lib/mailer_previews with the following content:

class UserMailerPreview < ActionMailer::Preview   def confirmation_instructions     UserMailer.confirmation_instructions(User.first, {})   end    def reset_password_instructions     UserMailer.reset_password_instructions(User.first, {})   end    def unlock_instructions     UserMailer.unlock_instructions(User.first, {})   end end 

So far, so good. Looks like I've done everything right. But then I try to see the preview for the confirmation_instructions email at the /rails/mailers/user_mailer/confirmation_instructions route and I get the following error:

undefined method `confirmation_url' for #<#<Class:0x007fa02ab808e0>:0x007fa030fb7e80> 

the code for my confirmation_url.html.erb template looks like this:

<%= t("notifications.texts.greeting") + @user.display_name %>,  <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> 

What am I doing wrong? I guess it is something wrong with the way I call the confirmation_url method. Anyone can help me here?

like image 557
rodrigoalvesvieira Avatar asked May 17 '14 21:05

rodrigoalvesvieira


People also ask

How do I Preview Mailer in Rails?

rails generates a mail preview if you use rails g mailer CustomMailer . You will get a file CustomMailerPreview inside spec/mailers/previews folder. Here you can write your method that will call the mailer and it'll generate a preview.

What is ActionMailer?

Action Mailer allows you to send emails from your application using mailer classes and views.


2 Answers

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

like image 115
steel Avatar answered Sep 22 '22 21:09

steel


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 
like image 24
eaton9000 Avatar answered Sep 22 '22 21:09

eaton9000