Hi! Thanks for visiting my blog. If you've received any value from my content would you mind supporting my new startup by downloading our browser add-on? It's called PriceBlink and makes online shopping a breeze. You can watch it in action here and download it for Chrome, Firefox, IE, or Safari by going to PriceBlink.com. Thank you and I hope you enjoy!

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 /] “);

Leave a Reply