How to control line breaks in plain text email views (.text.erb)
Asked Answered
B

2

9

Unlike most plain text email related questions, my problem is that there are too many line breaks in the plain text emails Rails is sending out.

For simplicity while starting up, I ditched HTML emails altogether and just use plain text emails (using .text.erb views). My problems occur where I have conditional lines in the view, as the new line of code in my view file carries over to the email.

For example:

    Line 1
    <%= "Line 2" if false %>
    Line 3

will render as:

    Line 1

    Line 3

and not the intended output:

    Line 1
    Line 3

My current hack is to use the following:

    Line 1
    <%= "Line 2\n" if false %>Line 3

This can get really messy when there are multiple conditionals in a row.

Surely there must be a better way!

Bozo answered 15/2, 2013 at 14:10 Comment(0)
G
4

This is to answer Felix' question on Andy Waite's answer (I don't think multi-line code is possible in comments and this question is about multi-line code).

I think <%= "foo\n" if something -%> would work, but this seems cleaner to me:

Line 1
<% if something -%>
foo
<% end -%>
Line 3
Greaten answered 1/8, 2013 at 4:56 Comment(0)
H
3

If you end the ERB tags with -%> that should avoid the extraneous whitespace:

<%= "foo" -%>
Host answered 15/2, 2013 at 14:36 Comment(2)
Would I need to do <%= "foo\n" if something -%> if I wanted the new line if and only if the text is inserted?Bozo
I think there is a cleaner way to do that, see my answer.Greaten

© 2022 - 2024 — McMap. All rights reserved.