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.
I wanted to allow my users to be able to include html tags in things they posted. Originally, I was thinking that this was done as an option to the form field, but it actually is in the view where you display the user content.
In my case, I have this in the form:
<%= f.text_area :content %>
and where you want to display it, just add “.html_safe”, for example:
<%= article.content.html_safe %>
I wanted to scroll some text and came across two options:
I ended up using html since it was easier and less to load on page load.
You can place text in fields to pre-populate them or to save space by putting the label in the field with simple html.
If you want label a field, you can use the placeholder tag which works in most modern browsers:
If you want to pre-populate a field, with text, you can use the value tag which will put the text, but NOT remove it when the focus is put on the field: