Preserve newline in text area with Ruby on Rails

前端 未结 2 1743
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 20:52

To practice Ruby on Rails, I am creating a blog which includes a text area (following Mackenzie Child\'s tutorial). When the text is submitted, all of the newlines are remov

2条回答
  •  情话喂你
    2020-12-15 20:58

    An easier (and dare I say better) way to handle this is to apply this CSS style to the paragraph or similar HTML element that you use to display user input inside of.

    white-space: pre-wrap;
    

    One advantage is this will persevere newlines just like simple_format without adding the extra formatting that it applies, such as turning two consecutive newlines characters into a paragraph element or automatically adding newlines to the end of the content. Just switched from using simple_format to this myself in a similar project. Way more predictable.

提交回复
热议问题