Take care of indentation and blank lines when using .erb for plain text emails
Building plain text emails with an .erb template doesn't allow you to indent code like you normally do in HTML mails.
DON'T
Copy<%= 'foo' if bar %>
"\n"
if bar is false
"foo\n"
if bar is true
Copy<%= nil %>
"\n"
Copy<% if true %> <%= 'foo' %> <% end %>
" foo"
Copy<%= 'foo' %> <%= 'bar' %>
"foo\n\nbar\n"
DO
Write unindented code to get the expected result.
Copy<% if bar %> <%= 'bar' %> <% end %> <%= 'foo' %> <%= 'bar' %>
- Use Form Models to move logic out of the template.
- Sometimes it makes sense to use .haml. It helps to write more beautiful code, but makes indentation more difficult.
Understanding trim mode for .erb
.erb has two approaches to handle leading spaces (<%- %>
) and trailing newlines (<% -%>
).
CopyERB.new(" <% %>foo").result => " foo" ERB.new(" <%- %>foo", nil, '-').result => "foo" ERB.new("<% %>\nfoo\n").result => "\nfoo\n" ERB.new("<% -%>\nfoo\n", nil, '-').result => "foo\n"
- You can combine removing leading spaces and removing trailing newlines.
- You need to enable trim mode for erb like above:
new(str, safe_level=nil, trim_mode=nil, eoutvar='_erbout')
Difference between Rails mailer and ERB
Rails mailer
Copy<% if true %> <%= 'bar' %> <% end %> <%= 'foo' %> <%= 'bar' %>
Copybar foo bar
ERB
Copy<% if true %> <%= 'bar' %> <% end %> <%= 'foo' %> <%= 'bar' %>
Copybar foo bar
Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).