EDIT:導軌UJS的link_to:遠程不AJAX GET和正常<a href> GET
In addition to the helpful comments below, two excellent articles by Steve Schwartz explain everything clearly:
- http://www.alfajango.com/blog/rails-3-remote-links-and-forms/
- http://www.alfajango.com/blog/rails-3-remote-links-and-forms-data-type-with-jquery/
滑軌3.2.2/jQuery的導軌2.0.1 (使用的jquery 1.7.1)
我有連結其發送一個AJAX請求以將其他文件上傳字段添加到表單。 與頁面相關的所有工作都能正常工作 - 新的表單字段HTML片段從服務器中檢索並附加到表單內的div上。
但是,
服務器收到兩個GET請求。一個是AJAX請求;另一個似乎是「正常的」錨元素GET。我預計「正常」的GET將被Rails UJS阻止。
我應該自己禁用正常的鏈接操作嗎?有人可以解釋我誤解了什麼,而我應該做什麼,而不是[阻止第二次請求]?
我看到這個問題:Rails 3 UJS - controller gets called twice by link_to :remote。因此,我試着改變資產管道編制config.assets.debug = false
,但它沒有效果。此外,這不是一個雙AJAX GET請求,所以我認爲這是一個不同的問題。
感謝您的幫助!
服務器日誌片斷:
Started GET "/files/new?asset_number=2" for 127.0.0.1 at 2012-03-23 15:23:27 +0100
Started GET "/files/new" for 127.0.0.1 at 2012-03-23 15:23:27 +0100
瀏覽器HTML:
<a href="/files/new" data-remote="true" id="add_another_file" position="after" update="files">Add another file</a>
查看:
<%= link_to 'Add another file', new_file_path, :remote => true,
:update => 'files',
:position => 'after',
:id => 'add_another_file' %>
控制器的CoffeeScript的:
$(->
$('a#add_another_file').click(->
url = '/files/new?asset_number=' + $('#files input').length
$.get(url, ((data) -> $('#files').append(data)), 'html')))
我跟着在Rails 3 In Action中提供了代碼。我認爲使用':remote'是正確的。如果不應該有一個手動'$ .get()',那麼我對它的工作原理感到困惑,我會調查一下。並感謝事件提示! – 2012-03-23 15:47:24
這個答案完全保存了我, - >使用骨幹事件+ jQuery綁定,瀏覽器處理它確定,ie8雖然沒有,因爲你的解釋。偉大的見解! – 2014-05-26 10:25:10