Question

I have a column in my database that contains a string like this:

"Warning set for 7 days.\nCritical Notice - Last Time Machine backup was 118 days ago at 2012-11-16 20:40:52\nLast Time Machine Destination was FreeAgent GoFlex Drive\n\nDefined Destinations:\nDestination Name: FreeAgent GoFlex Drive\nBackup Path: Not Specified\nLatest Backup: 2012-11-17"

I am displaying this data in an e-mail to users. I have be able to easily format the field in my html e-mails perfectly by doing the following:

simple_format(@servicedata.service_exit_details.gsub('\n', '<br>'))

The above code replaces the "\n" with "<br>" tags and simple_format handles the rest.

My issues arises with how to format it properly in the plain text template. Initially I thought I could just call the column, seeing as it has "\n" I assumed the plain text would interpret and all would be well. However this simply spits out the string with "\n" intact just as displayed above rather than created line breaks as desired.

In an attempt to find a way to parse the string so the line breaks are acknowledged. I have tried:

    @servicedata.service_exit_details.gsub('\n', '"\r\n"')
    @servicedata.service_exit_details.gsub('\n', '\r\n')
    raw @servicedata.service_exit_details
    markdown(@servicedata.service_exit_details, autolinks: false) # with all the necessary markdown setup
    simple_format(@servicedata.service_exit_details.html_safe)

none of which worked.

Can anyone tell me what I'm doing wrong or how I can make this work?

What I want is for the plain text to acknowledge the line breaks and format the string as follows:

Warning set for 7 days.
Critical Notice - Last Time Machine backup was 118 days ago at 2012-11-16 20:40:52
Last Time Machine Destination was FreeAgent GoFlex Drive

Defined Destinations:
Destination Name: FreeAgent GoFlex Drive
Backup Path: Not Specified\nLatest Backup: 2012-11-17"
Was it helpful?

Solution

I see.

You need to differentiate a literal backslash followed by a letter n as a sequence of two characters, and a LF character (a.k.a. newline) that is usually represented as \n.

You also need to distinguish two different kinds of quoting you're using in Ruby: singles and doubles. Single quotes are literal: the only thing that is interpreted in single quotes specially is the sequence \', to escape a single quote, and the sequence \\, which produces a single backslash. Thus, '\n' is a two-character string of a backslash and a letter n.

Double quotes allow for all kinds of weird things in it: you can use interpolation with #{}, and you can insert special characters by escape sequences: so "\n" is a string containing the LF control character.

Now, in your database you seem to have the former (backslash and n), as hinted by two pieces of evidence: the fact that you're seeing literal backslash and n when you print it, and the fact that gsub finds a '\n'. What you need to do is replace the useless backslash-and-n with the actual line separator characters.

@servicedata.service_exit_details.gsub('\n', "\r\n")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top