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?
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