如何开始使用 Rail 的 gem sanitize,因为我无法使他们的快速入门示例起作用?

How to get started with Rail's gem sanitize, because I'm not able to make their quick start example work?

我正在尝试使用富文本编辑器清理应用用户插入的一些内容。

我已经通过 bundle install

安装了 sanitize gem

它出现在已安装的 gem 列表中,然后在 application_controlled.rb

中添加了 require 'sanitize'

在我的一个视图中添加了此测试代码:<%= Sanitize.fragment('<p>1st <strong>sanitized </strong>comment</p>', Sanitize::Config::RELAXED) %> 但输出是 <p>1st <strong>sanitized </strong>comment</p>,我不知道为什么或如何解决它的问题。

没什么问题。 Sanitize::Config::RELAXED 允许 <p><strong>.

要完全清理字符串,请跳过第二个参数。

<%= Sanitize.fragment('<p>1st <strong>sanitized </strong>comment</p>' %>

=> `1st sanitized comment`

如果您担心字符串出现 "as is" 而没有转义标签,请将其标记为 html_safe

<%= Sanitize.fragment('<p>1st <strong>sanitized </strong>comment</p>', Sanitize::Config::RELAXED).html_safe %>

=> 第一条 已处理 评论