2011-09-29 54 views
3

我讀this issuejquery-rails,因爲我想在發送數據之前添加一些數據。Rails 3.1,jQuery:在發送數據前將數據附加到表單

說明看起來很簡單:掛在before:send上,並做你想要的形式。我就是這麼做的:

form.live "ajax:before",() -> 
    form.data('params', sortableList.nestedSortable('serialize')) 
    console.log "data we're sending along: " + form.data('params') # here I see the data I want to append to the form 

POST但不包含任何數據我加入到data-params的:

Started POST "/admin/site/nav_items/render_event_response?source=menu_item_tree&type=submit" for 127.0.0.1 at 2011-09-29 20:48:11 +0800 
    Processing by Admin::Site::NavItemsController#render_event_response as JS 
    Parameters: {"authenticity_token"=>"sV8eEMpIVicbmT0nAfDdChgpRMyI5yR/N6m5VLk3+/Q=", "source"=>"menu_item_tree", "type"=>"submit"} 
    User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1 
    ...snip... 

我才明白that issue錯了嗎? 如何在發送數據之前將數據附加到表單上?

謝謝!

+2

是越來越搞糊塗了,直到我意識到CoffeeScript的參與... – thomasfedb

回答

0

看那issue you are refering to

你錯過的重要組成部分。 ajax:before回調接受3個參數,您應該編輯其中的一個參數。

您的代碼應該是這樣的(可能是...我沒有測試過)

form.live "ajax:before", (event,xhr,settings) -> 
    settings.data('params', sortableList.nestedSortable('serialize')) 
相關問題