2011-01-27 12 views
6

我換成原型用jQuery爲Rails 3 我現在想做到以下幾點:的Rails 3 JQuery的page.insert_html和類型錯誤:Element.insert不是一個函數


    #photo 
    =render 'shared/photo' 
    = link_to_function "Add a Photo" do |page| | 
     page.insert_html :bottom, 'photo', :partial => 'shared/photo', :object => Photo.new | 
    end | 

所以JavaScript的產生是:



try { 
    Element.insert("photo", { 
     bottom: "Data\n\n" 
    }); 
} catch (e) { 
    alert('RJS error:\n\n' + e.toString()); 
    alert('Element.insert(\"photo\", { bottom: \"Data\\n\\n\" });'); 
    throw e 
}; 
return false; 

在軌文檔insert_html是一個原型幫手,但我認爲與https://github.com/rails/jquery-ujs更換Rails.js將取代這個助手jQuery的幫手。我得到一個TypeError.ElementInsert不是一個函數。

我做錯了什麼?或者我是否會在沒有幫手的情況下自己做這件事?

回答

3

有人只是把這個問題發給我,也許我可以幫忙。我在jquery-ujs/jquery-rails團隊中工作。

與rails的prototype-ujs不同,jquery-ujs沒有捆綁rjs支持。有一個單獨的gem提供了稱爲jrails的功能。

We've talked about merging the two projects,但決定讓它們分開,因爲它更靈活。

2

我有一個相當基本的解決方法。 在我的情況下,我運行了page.insert_html(:top ,'content', render('form'))

錯誤是由生成的.js.rjs代碼試圖運行Element.insert(),它是一個原型函數引起的。我不知道有足夠的瞭解軌道做任何事情非常有成效的,但添加以下到我的application.js已經形成的工作我:

//Check if the function exists 
if (typeof Element.insert !== "function") { 
    //If not, hook it onto the $().append method. 
    Element.insert = function (elem, ins) { 
    $("#" + elem).append(ins.top); 
    }; 
} 

顯然,這是基本的,只會一起工作:頂部,所以調整滿足您的需求。必須有更好的方法...

0

yes rails.js是一個js驅動程序,js在元素中做回調,
dom中的數據屬性。 (js提交,ajax鏈接等)

你需要的是重新定義原型軌道幫助程序,你不能 期望從js文件,除非它重新實現protptype語法la kdoole。

this fork of jrails提供你所需要的,並與軌道3

但它不使用jQuery爲我工作的作品。 我搜索了最新的,但沒有發現任何東西。

更新:我沒有管理,使其順利工作,是我的錯......

我想通大多數人使用js.erb模板使用jQuery這可能是我 所見過的最醜陋的代碼格式。

相關問題