Search code examples
htmlruby-on-railstextmailer

HTML vs text in rails app user_mailer


In my rails web app (based on the RoR tutorial by Michael Hartl) I have a user mailer which shoots out an activation email when someone signs up. There is an account_activation.*.erb for both HTML and text versions of the email. My account_activation.html.erb is

<h1>Web App</h1>

<p>Hi <%= @user.first_name %>,</p>

<p>Welcome to the Web App!</p>

<% if @user.student? %>
  <p>
    Once you have activated your account, you need to fill out the following information:
    <ul>
      ...
    </ul>
  </p>
<% elsif @user.teacher? %>
  <p>
    You have been created as a teacher. 
    After activation, you will need to reset your password as it has been
    randomly generated.
  </p>
<% end %>

<p>Click on the link below to activate your account:</p>

<%= link_to "Activate", edit_account_activation_url(@user.activation_token,
                                                    email: @user.email) %>

What I'm wondering is: what the corresponding account_activation.text.erb would be since I have a conditional statement in the HTML?


Solution

  • You can still use interpolated Ruby in your .text.erb files - it's ERB that handles that, not HTML. Your template should look pretty much the same, just laid out for text without any HTML tags.

    Since text file print all of your whitespace, you'll probably want to use <%- and -%> tags in some places, which respectively consume space before and after your interpolations. You'll also end up indenting less and not breaking things onto multiple lines, unless you use some tool to clean that up.

    Web App
    
    Hi <%= @user.first_name %>,
    
    Welcome to the Web App!
    
    <%- if @user.student? -%>
    Once you have activated your account, you need to fill out the following information:
    - One
    - Two
    <%- elsif @user.teacher? -%>
    You have been created as a teacher. After activation, you will need to reset your password as it has been randomly generated.
    <%- end -%>
    
    Click on the link below to activate your account:
    
    <%= link_to "Activate", edit_account_activation_url(@user.activation_token,
                                                        email: @user.email) %>