Stephen
Stephen

Reputation: 121

Detecting string that will render as nothing in Ruby on Rails

I have a rich text field that is sending data in an HTML format. I want to store it in a database, but only if it will actually render as something.

Example:

"hello world" => true
"<br><b></b>" => false
"<br><b>How are you today?</b>" => true

Upvotes: 3

Views: 94

Answers (1)

mrzasa
mrzasa

Reputation: 23317

Check if output of ActionView::Base.full_sanitizer.sanitize is blank:

ActionView::Base.full_sanitizer.sanitize("<b>").blank?
# => true
ActionView::Base.full_sanitizer.sanitize("<b> </b>").blank?
#=> true
ActionView::Base.full_sanitizer.sanitize("<b>a</b>").blank?
#=> false

https://stackoverflow.com/a/31180237/580346

Upvotes: 6

Related Questions