2010-08-29 61 views
4

我試圖將JQuery整合到Rails 3中。我已經從http://github.com/rails/jquery-ujs下載了rails.js文件,並將它包含在我的應用程序中。我還包括JQuery。Element.update不是函數[Rails 3 + JQuery]

但是,當我試圖做一個簡單的page.replace像這樣:

render :update do |page| 
    page.replace_html "my_div", :partial => "my_partial", :locals => {:mylocal => mylocal} 
end 

我得到的JavaScript拋出下面的錯誤執行:

RJS error: 
TypeError: Element.update is not a function 

Element.update("my_div", "mypartialdata"); 

任何想法?

回答

1

page.replace將致電prototype功能Element.update。你的頁面中是否包含原型?

-3

我想通了這個問題。顯然,使用JQuery,您不能使用「replace_html」或「insert_html」等rails助手。你只需要做頁面< <「// Jquery code」。

+0

遵循指引。 – GnrlBzik 2012-02-23 17:47:18

12

正如其他人所說,page.replace,在rails 2中,參考Element.update

如果你想使用jQuery的Rails 3提供給你同樣的幫手,看看jrails:

http://github.com/aaronchi/jrails

如果你希望使用Rails 3中的assert_select_rjs測試幫手,去抓住修補在此版本:

https://github.com/theworkinggroup/jrails

+0

很酷,很有效。謝謝。 – Bensign 2010-09-01 15:17:17

+0

謝謝它的作品! – Surya 2011-07-09 04:56:17

+0

但是jrails不能用於Rails 3:http://stackoverflow.com/a/7054825/429850 – highBandWidth 2013-02-13 07:53:23

0

如果真的是在使用jQuery軌道3使用render(:update)只是Element.update的事情,這點燃TLE的js代碼段應該這樣做:

Element = function(){} 
Element.update = function(id,html){$('#'+id).html(html);} 

解析這個JS代碼應該有所幫助。 我已經把它放在jquery_ujs.js文件的末尾。