Rails 4.1 Mailer Previews and Devise custom emails
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?
Preview devise mailer not found
Ok I found out why.
Because I'm using the gem rspec-rails
the repository test
is replaced tp spec
I just needed to move my test/mailers/previews/devise/mailer_preview.rb
to spec/mailers/previews/devise/mailer_preview.rb
and still not need to change the configuration of config/application.rb
Hope it will help some people
also note that in rails 6 your devise mailer preview should be wrote with a module
module Devise
class 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
end
Rails 4.1: access current_user in ActionMailer::Preview
What would happen if you move your mailer job to the background? How would you get the current user then?
The mailer and its preview should not know about the current_user
. The mailer's job is to send the mail to a user it receives. The preview is there to visually demonstrate its behaviour.
Create a new user in your mailer preview, and pass it to the mailer.
def new
user = User.create! # etc...
SubscriptionsMailer.new(user)
end
It doesn't matter who the user is. It matters that it's a user object.
If you want to test that the application will send a mail to the current_user
, write a functional test for that.
How do I preview emails in Rails?
Action Mailer now has a built in way of previewing emails in Rails 4.1. For example, check this out:
# located in test/mailers/previews/notifier_mailer_preview.rb
class NotifierPreview < ActionMailer::Preview
# Accessible from http://localhost:3000/rails/mailers/notifier/welcome
def welcome
Notifier.welcome(User.first)
end
end
Rails: I have devise confirmable installed but I want a seperate mailer for contact me and other features
Yes, you can have separate mailer, and it is better to have separate mailer instead of making devise handle all mail.
ActionMailer basics: http://guides.rubyonrails.org/action_mailer_basics.html
Related Topics
Is It Possible in Rails to Check Whether a Redirect or Render Had Already Been Issued
Rails Ssl Issue: (Https://Example.Com) Didn't Match Request.Base_Url (Http://Example.Com)
Ruby: How to Store and Display a Day of the Week
How to Highlight Fields on Rails Validation Errors
Rspec Testing Redirect to Url with Get Params
How Are Array.Each and Array.Map Different
How to Use a Branch in a Fork of Rails in a Project with Bundler
Select Mailbox "Sent Mail" or "All Mail" in Ruby Net::Imap
How to Change the Zone Offset for a Time in Ruby on Rails
How to Debug Ruby on Rails in Eclipse Aptana Plugin
How to Prevent Pipe Character from Causing a Bad Uri Error in Rails 3/Ruby 1.9.2
How to Get All Sundays Between Two Dates in Ruby
Convert Ruby to Low Level Languages
Extract Fast Fourier Transform Data from File
Ruby Symbols VS Strings in Hashes