Cameron Aziz
Cameron Aziz

Reputation: 487

Add new line using /n in text variable

I have a variable in my view printed on screen as

<%= comment.comment %>

but the output has many new line characters (\n).

How do I get it to actually print new lines whenever we have a \n with comment.comment output?

Upvotes: 3

Views: 796

Answers (4)

Prashant4224
Prashant4224

Reputation: 1601

Try this:

<%= comment.comment.dump %>

Upvotes: 0

Amadan
Amadan

Reputation: 198324

HTML interprets newlines as spaces except in elements those where CSS sets white-space to pre, pre-line or pre-wrap (like the <pre> element, by default).

Thus, you can do one of the following:

<pre><%= comment.comment %></pre>

<style> .with-newlines { white-space: pre } </style>
<div class=".with-newlines"><%= comment.comment %></div>

<%= comment.comment.gsub(/\n/, '<br>') %>

Upvotes: 0

osman
osman

Reputation: 2469

Its rendering HTML. So you need to convert the newlines into html tag br. Try simple_format http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format

Upvotes: 0

shivam
shivam

Reputation: 16506

Try this:

<%= comment.comment.gsub(/\n/, '<br />') %>

Else you can also use simple_format. Here:

<%= simple_format(comment.comment) %>

Upvotes: 3

Related Questions