传递特殊字符与observe_field的问题
问题描述:
我正在开发一个rails项目。使用标签observe_field,我将文本输入到文本区域,在控件中处理它,并将结果显示在div中(非常类似于堆栈溢出中的预览)。一切工作正常,直到我键入某些特殊字符。传递特殊字符与observe_field的问题
- ? =>使不要在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')})})
答
这是一个转义问题(如其他人所述)。
你想改变你的observe_field:with语句是这样的:
:with => "'postbody=' + encodeURIComponent(value)"
然后在你的控制器:
def textile_to_html
text = URI.unescape(params['postbody'])
...
答
生成的Javascript看起来像什么?
声音(乍一看)就像它没有被转义。