我目前有一个控制器从前端的 TinyMCE 捕获一些 html。如果我修补萤火虫,就可以提交脚本标签并将警报消息等注入到屏幕上。
编辑:目前我正在使用清理助手在模型中修复此问题:
require 'action_view'
class NotesController < AuthApplicationController
include ActionView::Helpers::SanitizeHelper
...
def update
params[:note][:content] = sanitize(params[:note][:content],
:tags => %w(a object p param h1 h2 h3 h4 h5 h6 br hr ul li img),
:attributes => %w(href name src type value width height data) );
@note.update_attributes(params[:note])
这在控制器中感觉很混乱。有没有更好的办法? IE。以某种方式集成此 ActiveRecord,以便我可以轻松指定在以类似于验证的方式保存之前对此字段和其他字段执行此操作?
感谢您的任何建议。
edit:
在这里取得一些进展。
在我的 /Libs 下我有
module SanitizeUtilities
def sanitize_tiny_mce(field)
ActionController::Base.helpers.sanitize(field,
:tags => %w(a b i strong em p param h1 h2 h3 h4 h5 h6 br hr ul li img),
:attributes => %w(href name src type value width height data) );
end
end
然后在我的模型中代码折叠为
class MyModel < ActiveRecord::Base
include ::SanitizeUtilities
...
before_save :sanitize_content
...
def sanitize_content
self.content = sanitize_tiny_mce(self.content)
end
end
这似乎可以毫不费力地去除不需要的标记。
对 Rails 还很陌生,所以很紧张,我可能做错了什么。有人能看到这里潜在的缺点吗?
再次感谢
我认为你这样做的方式很好,但是如果你使用before_save
那么你可能仍然会失败验证(因为before_save
验证后调用)。另外,您不一定必须将其放入自己的模块中,它可能只是您的类上的私有方法。
就像是:
class MyModel < ActiveRecord::Base
before_validation :sanitize_content, :on => :create
private
def sanitize_content
self.content = sanitize_tiny_mce(self.content)
end
def sanitize_tiny_mce(field)
ActionController::Base.helpers.sanitize(field,
:tags => %w(a b i strong em p param h1 h2 h3 h4 h5 h6 br hr ul li img),
:attributes => %w(href name src type value width height data) );
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)