method

strip_links

rails latest stable - Class: ActionView::Helpers::SanitizeHelper
strip_links(html)
public

Strips all link tags from html leaving just the link text.

strip_links('<a href="http://www.rubyonrails.org">Ruby on Rails</a>')
# => Ruby on Rails

strip_links('Please e-mail me at <a href="mailto:[email protected]">[email protected]</a>.')
# => Please e-mail me at [email protected].

strip_links('Blog: <a href="http://www.myblog.com/" class="nav" target=\"_blank\">Visit</a>.')
# => Blog: Visit.

strip_links('<<a href="https://example.org">malformed & link</a>')
# => <malformed & link

1Note

strip_links method not functioning in controllers, models, or libs

k776 ยท Apr 21, 2009

It comes up with an error about white_list_sanitizer undefined in the class you're using it in. To get around this, use:

ActionController::Base.helpers.strip_links('string')

To shorten this, add something like this in an initializer:

class String
def strip_links
  ActionController::Base.helpers.strip_links(self)
end
end

then call it with:

'string'.strip_links