我有这个 ruby 代码:
link_to t("general.actions.#{ action }"), [action, @app, item], {
:method => :put,
:remote => true,
:onclick => '$(this).closest("tr").find("td").fadeOut()'
}, :class => 'status'
生成此 html:
<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
来做到这一点?
最佳答案
使用 remote: true
,Rails 以 js
格式对 Controller 操作进行 ajax 调用。因此,当您准备好呈现输出时,在 Controller 的操作中,您只需指定 format.js
选项,如下所示:
def action
...
respond_to do |format|
format.html {...} # You probably have something like this already
format.js {} # Add this line
end
然后,您需要在您的app/views/app/
目录中添加一个action.js.erb
文件,并在onclick
那里。
关于ruby-on-rails - Rails 链接数据远程 ="true"和数据方法 ="PUT"后的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18317347/