caulfield
caulfield

Reputation: 1373

simple_format and 2+ newline(\n)

I'm using rails and need to show text, having 3 or more newline characters in a row.

I found simple_format method, but it works with 2,3,4,... symbols identically

Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped in < p > tags.

For example, my text is

1.9.3p0 :015 > Article.last.text
=> "1\n\n2\n\n\n\n33" 

when i do <%= simple_format Article.last.text.html_safe %> it generates me this view:

<p>1</p>
<p>2</p>
# but i need <br/> or smth else there
<p>3</p>

Other solutions are welcome, thanks.

Upvotes: 4

Views: 1868

Answers (1)

oldhomemovie
oldhomemovie

Reputation: 15129

I might still be missing something, but why not just use string.gsub(a, b):

"1\n\n2\n\n\n\n33".gsub("\n", "<br />").html_safe # => "1<br/><br/>2<br/><br/><br/><br/>33"

Surely you can also pass the previous line to simple_format to have the line wrapped into a <p> tag.

Upvotes: 7

Related Questions