Reusing Rails 2.2.2 strip_tag view helper
In one of my Rails projects, I have this one class that strips HTML from some user submitted text. Instead of reinventing the wheel, I reused the Rails view helper strip_tags() by including the relevant module:
class Presenter
include ActionView::Helpers::SanitizeHelper
end
This worked great until I upgraded the Rails framework to v2.2.2. The following error began appearing:
undefined method `full_sanitizer' for Presenter:Class
Turned out it was easy to fix. Looking at the strip_tags method in actionpack/lib/action_view/helpers/sanitize_helper.rb:
def strip_tags(html)
self.class.full_sanitizer.sanitize(html)
end
It's trying to access a class method that doesn't exist in Presenter. While I included the instance methods, now I also have to extend the necessary class methods.
class Presenter
include ActionView::Helpers::SanitizeHelper
extend ActionView::Helpers::SanitizeHelper::ClassMethods
end
Now all is good.