Is there a Railsy way to convert \n to <br>
?
Currently, I'm doing it like this:
mystring.gsub(/\n/, '<br>')
\r\n should probably do the trick.
"\n" is newline, '\n\ is literally backslash and n.
Yes, rails has simple_format
which does exactly what you are looking for, and slightly better since it also adds paragraph tags. See
http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format
Example:
simple_format(mystring)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With