Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there a Railsy way to convert \n to <br>?

Currently, I'm doing it like this:

mystring.gsub(/\n/, '<br>')
like image 925
daustin777 Avatar asked Oct 18 '22 02:10

daustin777


People also ask

How do you break a line in Ruby?

\r\n should probably do the trick.

How do you make a new line in Ruby?

"\n" is newline, '\n\ is literally backslash and n.


1 Answers

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.

like image 187
Daniel Von Fange Avatar answered Nov 06 '22 09:11

Daniel Von Fange