Add newline using / n in text variable
4 answers
Try the following:
<%= comment.comment.gsub(/\n/, '<br />') %>
Alternatively, you can also use simple_format
. Here:
<%= simple_format(comment.comment) %>
+1
source to share
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 to share
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 to share