DEV Community

loading...

Review Emails with ActionMailer::Preview

mikerogers0 profile image Mike Rogers 🛣 Originally published at community.codenewbie.org ・1 min read

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.

Discussion (0)

pic
Editor guide