How can I use strip_tags in regular Ruby code (non-rails)?

前端 未结 8 1721
广开言路
广开言路 2020-12-31 00:09

I need to turn HTML into plain text. There\'s a nice function that does that in ActionView\'s SanitizeHelper, but I have trouble understanding how I can reference it and use

8条回答
  •  暖寄归人
    2020-12-31 00:19

    Ideally you would require and include ActionView::Helpers::SanitizeHelper but there are several dependencies that don't get included when you do that. You can require them yourself to be able to use strip_tags.

    require 'erb'
    require 'active_support'
    require 'active_support/core_ext/class/attribute_accessors'
    require 'active_support/core_ext/string/encoding'
    require 'action_view/helpers/capture_helper'
    require 'action_view/helpers/sanitize_helper'
    
    include ActionView::Helpers::SanitizeHelper
    
    strip_tags("lol") # => "lol"
    

    This is assuming you have rails 3 gems installed.

提交回复
热议问题