ruby-on-rails - Rails 链接数据远程 ="true"和数据方法 ="PUT"后的回调

标签 ruby-on-rails ajax callback link-to link-to-remote

我有这个 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/

相关文章:

ruby-on-rails - 为 ruby​​ gem 的唯一性验证添加范围

javascript - 使用ajax上传图片

php - 返回 php 数组到 ajax 不工作

javascript - 在警报框中使用 AJAX

c# - 在 C# 中将回调机制实现为异步/等待模式

ruby-on-rails - Ubuntu 在代理问题后面安装 Nginx

ruby-on-rails - Ruby On Rails 如何与 Erlang 结合?

javascript - html2Canvas 无法及时检索数据 url 以生成 pdf

javascript - 如何在继续之前等待 Javascript 同步循环内的异步回调完成

ruby-on-rails - Net::FTPPermError (500 I won't open a connection to 10.10 ...... (only to 174.12 ......)