Strip html from string Ruby on Rails

后端 未结 6 1806
孤独总比滥情好
孤独总比滥情好 2020-12-07 16:35

I\'m working with Ruby on Rails, Is there a way to strip html from a string using sanitize or equal method and keep only text inside value attribute on input ta

6条回答
  •  無奈伤痛
    2020-12-07 16:55

    How about this?

    white_list_sanitizer = Rails::Html::WhiteListSanitizer.new
    WHITELIST = ['p','b','h1','h2','h3','h4','h5','h6','li','ul','ol','small','i','u']
    
    
    [Your, Models, Here].each do |klass| 
      klass.all.each do |ob| 
        klass.attribute_names.each do |attrs|
          if ob.send(attrs).is_a? String
            ob.send("#{attrs}=", white_list_sanitizer.sanitize(ob.send(attrs), tags: WHITELIST, attributes: %w(id style)).gsub(/

    \s*<\/p>\r\n/im, '')) ob.save end end end end

提交回复
热议问题