The problem is not so much in editing the value as in rendering it later. To add newlines to your value when editing in a text box, simply press the return key. When you re-edit this value later, spaces should still be there.
Rendering a space is the hard part. In HTML, spaces are usually not significant. A renderer similar to the one your browser uses displays one space for any continuous line of spaces. Therefore, simply resetting the value to the page will not be enough:
<%= obj.description %>
Even if your value may be "One \t \n \n Two" , it will be displayed on the screen as "One Two" .
So that these new line characters are actually split into lines when displayed, you need to convert them to HTML before rendering:
<%= obj.description.gsub(/\n/, '<br/>') %>
Of course, if users enter data that will be included in your HTML, you should avoid values to protect against XSS . If newlines are the only thing you need to maintain, it should be so simple:
<%= h(obj.description).gsub(/\n/, '<br/>') %>
If you want to allow more complex formatting, check out Markdown and Textiles (both of which Rails provide helper browsing methods). Just be sure to look into what if any support they provide to prevent XSS.
Ian Lesperance Jun 29 2018-10-10T00: 00-06
source share