Noah
Noah

Reputation: 964

Rails text field stored in DB with "\n" when output in ERB template not formatting right

I have the snippet below in an ERB template:

<% @task.notes.each do |note| %>
  <%= note.description %>
<% end %>

The text in one of the note's description is:

reply \n\nHello

But when it's output in the ERB template, it disregards the \n in the text.

Adding .html_safe to it does nothing.

How can I have it output with the \n?

Upvotes: 0

Views: 61

Answers (1)

MarsAtomic
MarsAtomic

Reputation: 10673

Take a look at the simple_format helper.

From the documentation:

Returns text transformed into HTML using simple formatting rules. Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped in <p> tags. One newline (\n) is considered as a linebreak and a <br /> tag is appended. This method does not remove the newlines from the text.

Does exactly what you want with no extra fuss.

Upvotes: 1

Related Questions