Follow Along as I Stumble on the Path to Learning RoR

Showing Line Breaks

I needed to show line breaks when displaying a text area field that users populated without having them add html.  For example, my app allowed for people to enter text which could span multiple paragraphs.  I didn’t want people to have to enter html, but I wanted it to show line breaks based on if user put line breaks (carriage returns) when they entered the paragraphs.

I found an answer on Stackoverflow.  When you want to display the text field, use code like this:

<%= h(obj.description).gsub(/\n/, ‘<br/>’).html_safe %>

where “obj” = your model, and “description” = your text field.

I also think that simple_format will work, as described here.


Comments are closed.