Review Emails with ActionMailer::Preview

mikerogers0

Mike Rogers ✈️

Posted on January 24, 2021

Review Emails with ActionMailer::Preview

Rails is one of my favourite frameworks, I think because it ships with a little known feature called ActionMailer::Preview, which I really want every Rails developer to know about :D

It renders your mailers & lets your preview them in the browser, which means if you want to tweak some code or totally redesign it, you can just visit http://127.0.0.1:3000/rails/mailers on your local development machine & get building.

Code Sample

# spec/mailers/previews/user_mailer_preview.rb
# Preview all emails at http://localhost:3000/rails/mailers
class UserMailerPreview < ActionMailer::Preview

  def user_welcome_notification
    UserMailer.user_welcome_notification(user)
  end

  private

  def user
    @user ||= FactoryBot.build(:user)
  end
end
Enter fullscreen mode Exit fullscreen mode

FactoryBot For Sample Data

I used to use User.first as the starting point for my sample data, but I think a better approach is to use the FactoryBot factories. The main advantage is it encourages me to write better factories & increases the value for money they offer.

💖 💪 🙅 🚩
mikerogers0
Mike Rogers ✈️

Posted on January 24, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related