Rails 3. simple_format do not wrap result in paragraph tags

前端 未结 3 1223
忘了有多久
忘了有多久 2020-12-20 12:28

How can I make simple_format not wrap the returned value in p tags?

simple_format \"*\"

相关标签:
3条回答
  • 2020-12-20 13:10

    Probably not what you really wanted, but... I ended up doing this:

    module ApplicationHelper
      def nl2br s
        split_paragraphs(sanitize(s, tags: [])).join('<br>').html_safe
      end
    end
    

    UPD Or better this:

    def nl2br s
      sanitize(s, tags: []).gsub(/\n/, '<br>').html_safe
    end
    
    0 讨论(0)
  • 2020-12-20 13:17

    Unfortunately -- you can't. If you check out the source at http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format you'll see that the p tags are wrapped around the content unconditionally.

    You could create a helper that uses the simple_format code, but modify it to not include the p tags...

    0 讨论(0)
  • 2020-12-20 13:22

    You can specify wrapper_tag option.

    simple_format 'Hello', {}, wrapper_tag: 'span'
    

    This code will be:

    <span>Hello</span>
    
    0 讨论(0)
提交回复
热议问题