One thing we didn’t look at was adding the confirmation email that is sent to the user to allow them to confirm their email address.

A couple of weeks ago we looked at Getting started with Trailblazer Cells. A Cell is basically a View Model that allows encapsulation for your views. This can often greatly reduce the complexity of messy views!

However, Cells can also be used for your emails. In today’s tutorial we will be looking at Cells and Action Mailer.

Getting up to speed

I’ve already mentioned where we left off from last week in the introduction to this post so I won’t go over those links again. If you have missed the last couple of weeks of posts it might be a good idea to have a quick glance over those posts now.

Generating the confirmation link

A couple of weeks ago I added a UserMailer class and a confirm_membership method for sending the confirmation emails. If you remember back, you can think of the UserMailer as essentially the same as a Controller.

One thing I didn’t do was to generate the actual confirmation link that will be in the email. Each confirmation link is unique and so it’s not something we can just hard code.

Now that we added the route in last week’s tutorial I can generate the link:

You might be wondering why I’m not generating the link inside of the Cell. Isn’t the whole point of using Cells to benefit from encapsulation? Normally I would of just generated the url from inside the Cell, but it’s a pain in the arse to do things like that in Rails because generating a url ends up touching a lot of core components of the framework.

Adding the concept helper method

Next we need to add a couple of things to make this thing work.

Firstly I’m going to add the email view:

= concept("confirmation/confirm/cell/email", @user, url: @url)

As you can see this is really simple because we’re just delegating to the Cell. Here I’m passing the user and the url from the UserMailer.

Next we can generate a preview of the email. This isn’t going to work because the Cell hasn’t be created yet, but we actually stumble across another problem first.

The concept helper function does not exist because Trailblazer only add it to the Controller and Action View. To fix this we can add a new method to the ActionMailer class that the UserMailer inherits from:

As usual we have the default show method. We also have a couple of methods that we will need in the view. Firstly we have a greeting method that will either return the users username or simply a placeholder string. And secondly we have a url method that will generate the link and add the QA class for us.

Adding the template

Now that we have the Cell in place we can also add the template:

p Hey #{greeting}!
p Click #{url} to confirm your account!

As you can see this is really simple for now. I hate having to deal with HTML emails and so I’ll kick that can down the road and deal with it another day. This very simple template will do for now.

If you fire up the preview again you should see it working correctly including the greeting and the url.

Adding some tests

Finally we can add a couple of tests to ensure that the HTML is generated correctly: