Follow Along as I Stumble on the Path to Learning RoR

Posts tagged “text field

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.


Default Values and Placeholders in Text Fields

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: