Search code examples
ruby-on-railsruby-on-rails-3html-safe

How to remove HTML markup from string


Let's say I have:

@string = "it is a <a href="#">string</a>"

I want to use it in different parts of my application in two ways:

  • With a clickable link
  • Without the clickable link (but not showing any HTML markup)

The first one can be done using html_safe:

@string.html_safe

It is a string

How can I achieve the second one?

It is a string.


Solution

  • You can try this:

    ActionView::Base.full_sanitizer.sanitize(@string)
    

    See strip_tags(html).