ruby on rails - How to display text without its css -
i'm working ruby on rails , in app there's text area tinymce in it, users can add styles text, , upload images , videos.
when these displayed in listings on home page, don't want posts' styles shown, nor videos/images well.
for example, let's write:
how doing? (some image/video here)
then, simple show in listings following:
how doing? (no image/video shown)
you have several options can use. stripping, scrubbing html comes useful when have render screen scrapped web content. case might simpler, don't "expect unexpected" content gathered web crawls.
you can use strip_tags
removes tags string:
strip_tags("strip <i>these</i> tags!") # => strip these tags! strip_tags("<b>bold</b> no more! <a href='more.html'>see more here</a>...") # => bold no more! see more here... strip_tags("<div id='top-bar'>welcome website!</div>") # => welcome website!
the sanitize
method should escape , remove other tags. more rails sanitization, go here.
if need more control check out sanitize or loofah. prefer loofah, sanitize might meet tastes maybe more. loofah built on nokogiri, can define fine grained rules how want massage html or html fragments. except of whitelisting, stripping can scrubbing of tags:
span2div = loofah::scrubber.new |node| node.name = "div" if node.name == "span" end
.. changes spans divs.
Comments
Post a Comment