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

后端 未结 8 1258
情书的邮戳
情书的邮戳 2020-12-04 11:30

Is there a Railsy way to convert \\n to
?

Currently, I\'m doing it like this:

mystring.gsub(/\\n/, \'
\')
相关标签:
8条回答
  • 2020-12-04 12:00

    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.

    0 讨论(0)
  • 2020-12-04 12:02

    Nope. What you have there is the commonly used alternative. The definition most people use is:

       def nl2br text
           text.gsub(/\n/, '<br/>')
       end
    

    It is named as such because it mimics the functionality of the PHP function by the same name.

    0 讨论(0)
提交回复
热议问题