2008-08-27 17 views
1

我正在開發一個rails項目。使用標籤observe_field,我將文本輸入到文本區域,在控件中處理它,並將結果顯示在div中(非常類似於堆棧溢出中的預覽)。一切工作正常,直到我鍵入某些特殊字符。傳遞特殊字符與observe_field的問題

  1. ? =>使不要在PARAMS中發現的可變對象
  2. (磅)=>導致一個無效的真實性錯誤
  3. %=>停止從被更新
  4. & =每一件事情>後&是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')})}) 

回答

3

這是一個轉義問題(如其他人所述)。

你想改變你的observe_field:with語句是這樣的:

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

然後在你的控制器:

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

你可以提供一個代碼示例?

更有可能你只需要使用encodeuri或類似的東西來逃避你的HTML實體。

0

生成的Javascript看起來像什麼?

聲音(乍一看)就像它沒有被轉義。