In Rails - is there a rails method to convert newlines to <br>?

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


Is there a Railsy way to convert \n to <br>?

Currently, I'm doing it like this:

mystring.gsub(/\n/, '<br>')

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Yes, rails has simple_format which does exactly what you are looking for, and slightly better since it also adds paragraph tags. See

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format

Example:

 simple_format(mystring)

Note that simple_format allows basic HTML tags, but also passes text through sanitize which removes all scripts, so it should be safe for user input.

Solution 2 - Ruby on-Rails

You may make it more general by doing:

mystring.gsub(/(?:\n\r?|\r\n?)/, '<br>')

This way you would cover DOS, *NIX, Mac and accidental invalid line endings.

Solution 3 - Ruby on-Rails

You should be careful with this when you are dealing with user input.
simple_format inserts <br> tags but it will allow other html tags!

When using simple_format, <b>Hello</b> will be rendered as "Hello", you might not want this.

Instead you can use <%= h(c.text).gsub("\n", "<br>").html_safe %>
h() will encode the html first, gsub replaces the line break and html_safe allows the <br> tags to be displayed.

This will display exactly what the user entered. It also allows to discuss html in e.g. comments.

Solution 4 - Ruby on-Rails

Simply use

white-space: pre-line;

in your css and text will wrap on line breaks.

Solution 5 - Ruby on-Rails

You also might consider what you're trying to do - if you're nicely formatting text that people have entered, you might consider a filter like Markdown to let your users format their text without opening up the can of worms that is HTML. You know, like it is here at Stack Overflow.

Solution 6 - Ruby on-Rails

Nope. What you have there is the commonly used alternative. The definition most people use is:

   def nl2br text
       text.gsub(/\n/, '<br/>')
   end

It is named as such because it mimics the functionality of the PHP function by the same name.

Solution 7 - Ruby on-Rails

mystring.gsub(/\r\n|\r|\n/, '\n')

worked for me

Solution 8 - Ruby on-Rails

You can do simple_format(h(text)) – the h will ensure any HTML is not rendered.

As mentioned in other answers, this will do a bit more than you asked for. It wraps the whole thing in <p>, and adds more paragraphs if you have double newlines anywhere.

Solution 9 - Ruby on-Rails

Another option is just to show the text inside <pre> html5 tag.

> The HTML

 element represents preformatted text which is to be
> presented exactly as written in the HTML file. The text is typically
> rendered using a non-proportional ("monospace") font. Whitespace
> inside this element is displayed as written.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondaustin777View Question on Stackoverflow
Solution 1 - Ruby on-RailsDaniel Von FangeView Answer on Stackoverflow
Solution 2 - Ruby on-RailsTomalakView Answer on Stackoverflow
Solution 3 - Ruby on-RailsjomoView Answer on Stackoverflow
Solution 4 - Ruby on-RailsbershikaView Answer on Stackoverflow
Solution 5 - Ruby on-RailsJim PulsView Answer on Stackoverflow
Solution 6 - Ruby on-RailsryeguyView Answer on Stackoverflow
Solution 7 - Ruby on-Railsuser7516728View Answer on Stackoverflow
Solution 8 - Ruby on-RailsHenrik NView Answer on Stackoverflow
Solution 9 - Ruby on-RailsKiryl PlyashkevichView Answer on Stackoverflow