nl2br in RoR
Posted by Gerald Abrencillo on January 31, 2008
Filed Under Bugs and Fixes, Ruby on Rails
I recently found out that line breaks are not recognized when displaying a large block of text on the view (e.g. a post body). In PHP this was handled by the nl2br method but I had no luck finding a similar function in RoR. So I did what any sane person would do, create one myself. I placed this method in our model to format a particular field but it seems a better choice to put it in a helper.
def nl2br(text)
return text.gsub(/\n/, ‘<br/>’)
end
And there you have it, your very own nl2br method. Either you just found something really useful or I just wasted 2 minutes of your time. If anyone has a better solution or if I missed a function for this let us know by dropping a comment.
Comments
-
maximus
-
Gerald Abrencillo
-
mikong
-
Gerald Abrencillo
-
Ramon Tayag