Rails默认用 h 来转义html,如果我们希望输出带有html格式的信息,此时应加上sanitize方法,它可以吧<form> 和 <script>、on=、javascript:等危险标签统统去掉!
一条原则:不用h就用 sanitize!
Sanitize太彻底了,去掉了所有的html标签,包括图片。
自定义选项
通过查询API,
<%= sanitize @article.body, :tags => %w(table tr td), :attributes => %w(id class style) %>
比如,对于商铺简介,显示方式为:
<%= sanitize @league.intro, :tags => %w(p img), :attributes => %w(style src height width alt ) %>