Is there a Railsy way to convert \n to <br>?
Currently, I’m doing it like this:
mystring.gsub(/\n/, '<br>')
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, rails has
simple_formatwhich does exactly what you are looking for, and slightly better since it also adds paragraph tags. Seehttp://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format
Example:
Note that
simple_formatallows basic HTML tags, but also passes text throughsanitizewhich removes all scripts, so it should be safe for user input.