2013-08-19 25 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