2013-08-19 23 views
2

我有這樣的紅寶石代碼:回調與鏈接數據的遠程=「true」和數據方法=「PUT」

link_to t("general.actions.#{ action }"), [action, @app, item], { 
    :method => :put, 
    :remote => true, 
    :onclick => '$(this).closest("tr").find("td").fadeOut()' 
}, :class => 'status' 

產生這個網站:

<a href="/apps/guess-the-model/contents/52118140d644363dc4000005/approve" 
    data-method="put" 
    data-remote="true" 
    onclick="$(this).closest("tr").find("td").fadeOut()" 
    rel="nofollow"> 
    approve 
</a> 

問題是的,我不應該使用onclick。我應該只在AJAX成功時調用這個js代碼,所以我應該通過回調並檢查XMLHttpRequest狀態。我如何使用:remote => true:method= :put來做到這一點?

+0

我想,你應該在方法的js模板中寫入這個js,它被遠程鏈接調用。 –

回答

4

使用remote: true,Rails以格式js向您的控制器操作發出ajax調用。所以,在你的控制器的動作,當你準備渲染,你只需要如下指定format.js選項輸出:

def action 
    ... 

    respond_to do |format| 
    format.html {...} # You probably have something like this already 
    format.js {} # Add this line 
end 

然後,你需要添加一個action.js.erb文件在您app/views/app/目錄並執行JavaScript代碼你想在那裏執行onclick