在Rails控制器中使用清理
我试图在控制器中调用sanitize
。 这是我尝试过的:
class FooController < ApplicationController include ActionView::Helpers::SanitizeHelper # ... end
但是,我收到此错误:
undefined method `white_list_sanitizer' for FooController:Class
我四处搜索,人们建议切换include行以包含ActionView::Helpers
,但这会导致此错误:
undefined method `url_for' for nil:NilClass
呼叫sanitize
的正确方法是什么? 我正在使用Rails 2.3.5。
你可以在action方法中使用这个ActionController::Base.helpers
:
class SiteController < ApplicationController def index render :text => ActionController::Base.helpers.sanitize('bold') end end
希望这可以帮助
我不确定你在这里要做什么,但我几乎100%肯定它不属于控制器。
如果要在将属性保存到数据库之前清理属性,请在模型中使用之前的保存回调进行清理。
否则,在视图模板或视图助手中进行清理。