Rails -- h.simple_format 修复

Rails -- h.simple_format fix

这是我的代码:

 = h.simple_format(item.text.text, class: 'basic')

item.text.text"<p><span style=\"color: rgb(251, 213, 181);\">asdasdasd</span></p>",但是当我看到在 inspect 中呈现的视图时,style=\"color: rgb(251, 213, 181);\" 属性消失了!

如何解决这个问题! (注意:我需要class: 'basic'!)

simple_format 来自 ActionView::Helpers::TextHelper 具有默认选项 sanitize: true

Sanitizes the html by converting and tags into regular text, and removing all "onxxx" attributes (so that arbitrary Javascript cannot be executed). It also removes href= and src= attributes that start with "javascript:". You can modify what gets sanitized by defining VERBOTEN_TAGS and VERBOTEN_ATTRS before this Module is loaded.

防止删除样式属性:

simple_format(item.text.text, { class: 'basic' }, sanitize: false)

<p>里面的<p>在HTML的所有标准中都是无效的,因为开始<p>标签会自动关闭<p>元素:

#item.text.text => "<p><span style=\"color: rgb(251, 213, 181);\">asdasdasd</span></p>"

simple_format(item.text.text, { class: 'basic' }, sanitize: false, wrapper_tag: "div")
#=> "<div class=\"basic\"><p><span style=\"color: rgb(251, 213, 181);\">asdasdasd</span></p><div>"