---
title: "过滤HTML字符--sanitize"
author: "muyu"
date: "2008-10-06T17:45:45Z"
tags: []
slug: "html-sanitize"
draft: false
lastmod: "2008-10-06T17:45:45Z"
id: 249507
url: "https://muyu.iteye.com/blog/249507"
---

Rails默认用 h 来转义html，如果我们希望输出带有html格式的信息，此时应加上sanitize方法，它可以吧&lt;form&gt; 和 &lt;script&gt;、on=、javascript:等危险标签统统去掉！

 

一条原则：不用h就用 sanitize！

 

Sanitize太彻底了，去掉了所有的html标签，包括图片。

 

自定义选项

 

通过查询API，

 

&lt;%= sanitize @article.body, :tags =&gt; %w(table tr td), :attributes =&gt; %w(id class style) %&gt;

 

比如，对于商铺简介，显示方式为：

 

&lt;%= sanitize @league.intro, :tags =&gt; %w(p img), :attributes =&gt; %w(style src height width alt ) %&gt;