使用observe_field传递特殊字符时出现问题
时间:2020-03-05 18:44:08 来源:igfitidea点击:
我正在从事Rails项目。使用标签observe_field,我将输入文本的文本放入文本区域,在控件中对其进行处理,并将结果显示在div中(非常类似于堆栈溢出中的预览)。一切正常,直到我键入某些特殊字符为止。
- ? =>导致在params对象中找不到该变量
- (磅)=>导致无效的真实性错误
- %=>阻止div更新
- &=>&之后的所有内容都不再传递到服务器上的变量中。
有办法解决吗?
-代码示例-
这是视图。 (" postbody"是一个文本区域)
<%= observe_field 'postbody', :update => 'preview', :url => {:controller => 'blog', :action => 'textile_to_html'}, :frequency => 0.5, :with => 'postbody' -%>
这是被称为控制器
def textile_to_html text = params['postbody'] if text == nil then @textile_to_html = '<br/>never set' else r = RedCloth.new text @textile_to_html = r.to_html end render :layout => false end
这是创建的javascript:
new Form.Element.Observer('postbody', 0.5, function(element, value) {new Ajax.Updater('preview', '/blog/textile_to_html', {asynchronous:true, evalScripts:true, parameters:'postbody=' + value + '&authenticity_token=' + encodeURIComponent('22f7ee12eac9efd418caa0fe76ae9e862025ef97')})})
解决方案
回答
我们可以提供代码示例吗?
我们很有可能只需要使用encodeuri或者类似的方法来转义HTML实体。
回答
生成的Javascript是什么样的?
听起来(乍一看)好像没有被逃脱。
回答
这是一个逃避的问题(如其他人所述)。
我们将需要将observe_field:with语句更改为:
:with => "'postbody=' + encodeURIComponent(value)"
然后在控制器中:
def textile_to_html text = URI.unescape(params['postbody']) ...