Skip to content

Getting Started with Rails 7 Episode 25: Set Up a Comments Mailer

This is the 25th video in the Getting Started with Rails 7 series. In this video Mike generates and implements a mailer that sends an email when a comment is created or destroyed and works through testing the mailer and making sure it doesn’t break the browser tests.

There are a couple of “gotchas” illustrated in this video:

  • In the destroyed mail, you can’t use the Comment object in a deliver_later context because the mailer will not be able to serialize the destroyed object.
  • If you are using a partial shared by the view and the mailer be aware of things that may not work in both contexts (like linking to a _path instead of a _url). We work around this by refactoring the attributes portion of the original partial into a smaller partial that is shared by the mailer and original partial

This video covers:

  • 00:00:10 Introduction
  • 00:02:26 Generate the mailer and explore what gets generated
  • 00:09:13 Adapt the tests to our use case
  • 00:12:33 Incrementally develop mailer to make mailer tests pass
  • 00:20:50 Adapt mailer preview and review previews
  • 00:23:10 Add dynamic comment information into mailer views using partials
  • 00:38:07 Invoke mailers in controller using deliver_later
  • 00:40:41 Destroy mailer does not serialize in deliver_later because record is no longer available. Rework to pass attributes to the mailer
  • 00:48:25 Fix error in CommentControllerTest
  • 00:53:07 Test that destroy email gets sent in the application
  • 00:54:36 Fix regression surfaced by system tests. The is_email solution doesn’t work with the Turbo Stream. Refactor attributes into their own partial that gets called by the emails
  • 01:04:29 Validate in application, commit, and push code

The code for this series is open source and available on GitHub. View the commit for this video (shared commit between this video and the previous video) here.

Leave a Reply