daustin777 daustin777 - 1 year ago 46
Ruby Question

In Rails - is there a rails method to convert newlines to <br>?

Is there a Railsy way to convert \n to


Currently, I'm doing it like this:

mystring.gsub(/\n/, '<br>')

Answer Source

Yes, rails has simple_format which does exactly what you are looking for, and slightly better since it also adds paragraph tags. See




Note that simple_format allows basic HTML tags, but also passes text through sanitize which removes all scripts, so it should be safe for user input.