Its all about Ruby On Rails
converting all newline characters to br tag
I was surprised as there is no function in ruby to convert all newline characters to <br>.
Here is a php nl2br equivalent method to convert all newline characters (\n) to break tag (<br>) in a string.
def nl2br(s)
s.gsub(/\\n/, '<br>')
end
| Print article | This entry was posted by Akhil Bansal on August 7, 2006 at 10:48 am, and is filed under ROR, Rails, Rubyonrails. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 4 years ago
def nl2br(s)
s.gsub(/\n/, ”)
end
This would be more appropriate
about 4 years ago
This will remove all new line characters not replace them by <br>
about 3 years ago
I believe he was reffering to the /\n/ part. Since /n/ will remove all n-characthers and replace them with
about 3 years ago
simple_format text helper do the trick
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M000623