最佳答案
我正在编写一个模型来处理来自文本区域的用户输入。遵循来自 http://blog.caboo.se/articles/2008/8/25/sanitize-your-users-html-input的建议,在保存到数据库之前,我将使用 before _ validalback 清理模型中的输入。
The relevant parts of my model look like this:
include ActionView::Helpers::SanitizeHelper
class Post < ActiveRecord::Base {
before_validation :clean_input
...
protected
def clean_input
self.input = sanitize(self.input, :tags => %w(b i u))
end
end
Needless to say, this doesn't work. I get the following error when I try and save a new Post.
undefined method `white_list_sanitizer' for #<Class:0xdeadbeef>
显然,SanitieHelper 创建了一个 HTML: : WhiteListSanitizer 的实例,但是当我将它混合到我的模型中时,它找不到 HTML: : WhiteListSanitizer。为什么?我该怎么做才能修好它?