Convert line ending to <br>

This is more of a ruby question, but I have a textarea in a form, and for later presentation I need to convert the line endings to <br>.

The value from the form is, for example "line of text\r\nmore text". I tried gsub('\r\n', "<br>"), but it doesn't find the \r\n. How do I specify those characters?

Thanks, -George

You would need to use double quotes around \r\n, "\r\n", or express it as a regex, /\r\n/.

But see also the built-in simple_format helper.

That was it. Thanks!

-George

Thanks. I added that after the \r\n line. I figured one of them has to work.

But then I tried out the simple_format that Bob Showalter suggested, which seems to be a better way to do it.

use this converting all newline characters to br tag | WebOnRails

rails has its own simple_format function, it may meet your need.

simple_format(text) Returns text transformed into HTML using very simple formatting rules Surrounds paragraphs with &lt;p&gt; tags, and converts line breaks into &lt;br /&gt; Two consecutive newlines(\n\n) are considered as a paragraph, one newline (\n) is considered a linebreak, three or more consecutive newlines are turned into two newlines