Reputation: 487
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
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
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
Reputation: 16506
Try this:
<%= comment.comment.gsub(/\n/, '<br />') %>
Else you can also use simple_format
. Here:
<%= simple_format(comment.comment) %>
Upvotes: 3