Ruby on Rails Replacing Line Feed with HTML Equivalent
Aug 31
This is a Ruby snippet that’s simple and something that I’ve found useful. When allowing users to enter multiline data in your web application, say through a textarea, there will most likely be line feeds. When a user hits the enter key they will expect the cursor to move to the next line. This is default textarea behavior, but what happens when this is stored to the database and then displayed on the a web page?
In most instances the database will save the line feed as the \n character. As you most likely know, the web browser will ignore this line feed. So a simple fix is to replace line feeds with their HTML equivalents. The following snippet will do the trick:
@body.gsub(“/n”, “[br /] “);
Read More
