使用 observe_field 传递特殊字符时出现问题

Problems passing special chars with observe_field

提问人:wusher 提问时间:8/28/2008 最后编辑:wusher 更新时间:11/14/2008 访问量:463

问:

我正在从事一个轨道项目。使用标记observe_field,我将键入到文本区域中的文本,在控件中对其进行处理,并在 div 中显示结果(非常类似于堆栈溢出中的预览)。在我输入某些特殊字符之前,一切正常。

  1. ?=> 导致在 params 对象中找不到变量
  2. (磅) => 导致无效的真实性错误
  3. % => 停止更新 div
  4. & => & 之后的所有内容都不再传递到服务器上的变量中。

有没有办法解决这个问题?

---代码示例---

这是观点。(“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')})})
Ruby-on-Rails 阿贾克斯

评论


答:

0赞 Orion Edwards 8/28/2008 #1

你能提供一个代码示例吗?

更有可能的是,您只需要使用 encodeuri 或类似的东西来转义您的 HTML 实体。

0赞 nikz 9/1/2008 #2

生成的 Javascript 是什么样子的?

听起来(乍一看)好像它没有被逃脱。

3赞 waldo 11/14/2008 #3

这是一个逃避问题(如其他人所述)。

您需要将observe_field :with 语句更改为:

  :with => "'postbody=' + encodeURIComponent(value)"

然后在控制器中:

def textile_to_html
  text = URI.unescape(params['postbody'])
  ...