Using Multiple Templates with the mail_form Gem

Dana Scheider (he/they)
3 min readJun 1, 2018

I’ve been working on InView’s informational site and have run into a special use case. It’s a mostly static site with two contact forms and we’re using the mail_form gem to handle e-mails from the forms. For the most part, the default mail_form contact form works for the e-mails that get sent to us. However, we also wanted to send an e-mail to each person who submits the form inquiring about how to get involved with the project, letting them know we got their e-mail and explaining next steps. How to do this with mail_form turned out to be quite simple but non-obvious.

If you’re not familiar with the mail_form gem, I encourage you to read a tutorial before jumping into this, since there’s ample information out there about the basics and I don’t want to rehash it all here.

InView’s Contact Form

For our purposes, we’re going to be looking at our contact form for engineering users. This form is quite simple:

There are two classes associated with this form: The EngineeringContact class and the EngineeringContactsController that actually validates the form and sends the e-mail when the form is submitted. This is the basic code in this class, which is in the app/models directory.

Then the controller action is:

Adding the Confirmation E-mail

When a user submits the form, all their information gets sent to InView. However, the user also needs to know what the next steps are for them to be onboarded. For that reason, we needed to send an additional e-mail when the engineering form was submitted. This started with creating a new model, which we called EngineeringUser.

Since this e-mail would be sent when the user submitted the form, we didn’t need an additional controller for this, so we edited the create action in the EngineeringContactsController.

Here, we create an engineering user and send an e-mail to them. However, there’s still one problem with this: The e-mail the user receives will simply contain a list of their attributes — their name, GitHub user ID, and e-mail. We need them to get an onboarding e-mail.

The Solution

Although the mail_form gem doesn’t document an approach to solving this problem, it actually provides a very easy way. First, you’ll need to run (from the project directory) mkdir app/views/mail_form. Inside this directory, create the template you’d like. For us, we called this app/views/mail_form/onboarding_form.html.erb.

Within the view you create, the contact is called @resource, and you can access its attributes using accessor methods. So for instance, the opening of our e-mail reads Hi <%= @resource.name %>!.

Connecting this view to the correct model is, likewise, quite simple. All you need to do is edit the headers method on the model, adding the key :template_name to the hash.

And there you have it! The e-mail sends as expected.

--

--

Dana Scheider (he/they)

Senior Engineer at CashApp, formerly at Envato & New Relic