Add newline using / n in text variable

I have a variable in my view, printed to the screen as

<%= comment.comment %>

      

but there are many new string characters ( \n

) in the output .

How do I get it to actually print new lines whenever we have output \n

from comment.comment

?

+3


source to share


4 answers


Try the following:

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

      



Alternatively, you can also use simple_format

. Here:

<%= simple_format(comment.comment) %>

      

+1


source


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



0


source


HTML interprets newlines as spaces, except for elements where CSS sets white-space

to pre

, pre-line

or pre-wrap

(for example, the <pre>

default element ).

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>') %>

      

0


source


Try the following:

<%= comment.comment.dump %>

      

0


source







All Articles