model的after_save前sanitize html

ActionView::Helpers::SanitizeHelper中有sanitize方法,可以在view 中使用

如果想在model中使用,可以include ActionView::Helpers::SanitizeHelper. 但是会覆盖掉ActiveRecord::Base的sanitize方法

 

还有一个办法:

after_save :sanitize_html

private
  def sanitize_html
    sanitizer = HTML::WhiteListSanitizer.new
    self.body = sanitizer.sanitize(self.body)
  end
 

你可能感兴趣的:(html,ActiveRecord)