27

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.

1

7 Answers 7

54

You can try this:

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

See strip_tags(html).

Sign up to request clarification or add additional context in comments.

1 Comment

Please note that this also adds HTML markup. It converts & to &amp;
11

You can try this:

strip_tags(@string)

Comments

7

For general-purpose use (e.g. web scraper):

puts Rails::Html::FullSanitizer.new.sanitize("<div>Hello</div><br>")
# Hello

Comments

3

You can use nokogiri to do the same.

This SO post tells the story.

Here in short:

This uses the XPath's starts-with function:

You have to first define it like this:

require 'nokogiri'

item = Nokogiri::HTML('<a href="#">string</a>')
puts item.to_html

The above will give the html output. Then you can use XPath.

item.search('//a[not(starts-with(@href, "http://"))]').each do |a|
  a.replace(a.content)
end
puts item.to_html

Comments

1

In Rails, see also the strip_tags method. http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-strip_tags

Comments

0

Rails provides a method called strip_links, which seems to do what you want (looking at its name).

According to its APIDock page it is a bit limited. To make it applicable to a/any string you could extend the string class:

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

So you can use:

@string.strip_links

2 Comments

strip_links gives an error if the string has no html markups. Extending the method don't give the error, but does not work for some markups, such as <em>. But thanks anyway.
Oh... I thought/assumed you always have a link in your string... I guess the sanitize method removes all HTML... (It is in the same Helper module)
0

Inspired by upstairs, I define this function in my project

 def delete_html_markup(data)
    return data if data.blank?
    if data.is_a?(Array)
      data.map{ | s |  delete_html_markup(s)  }
    elsif data.is_a?(Hash)
      data.each do | k, v |
        data[k] = delete_html_markup(v)
      end
    else
      ActionView::Base.full_sanitizer.sanitize(data)
    end
  end

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.