如何在Rails中异步执行Controller.destroy操作? [英] How to perform Controller.destroy action asynchronously in rails?

查看:103
本文介绍了如何在Rails中异步执行Controller.destroy操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过以下代码轻松地删除Rails应用程序中 post c的条目:



视图



 <%@ post.each do | post | %> 
< tr>
< td colspan = 3><%= link_to到销毁,发布,:method => :delete,:confirm => ‘确定吗?’,:class => ‘btn btn-mini btn-danger’%>< / td>
< / tr>
<%end%>



控制器类



  def destroy 
@post = Post.find(params [:id])
@ post.destroy
response_do | format |
format.html redirect_to(posts_url)}
format.xml {head:ok}
结束
结束

这很好。如何使此破坏进程异步。因此,我不需要添加 route 刷新页面



编辑



添加更多说明



任务是使破坏方法 async 。这样客户端就不会等待响应,并且 destroy 方法将在后台 async 中运行。此外,destroy方法没有保证的完成时间。



另一个例子可能是:我必须一次删除多个帖子。

解决方案

要发出异步请求,您需要 Ajax 和基本的 javascript 删除< td> 的代码。



首先将您的视图代码更改为:

 <%@ post.each do | post | %> 
< tr>
< td colspan = 3><%= link_to到销毁,发布,:method => :delete,id:post.id,:class => btn btn-mini btn-danger,数据:{确认:您确定吗?,远程:true}%> < / td>
< / tr>
<%end%>

上面的代码表示它将调用 destroy PostController 类的方法。现在,您需要对控制器类的 destroy 方法进行一些更改

  def销毁
@post = Post.find(params [:id])
@ post.destroy
response_to | format |
format.js
format.html {redirect_to(posts_url)}
结束
结束

上面的代码是可以理解的,除了为什么我们使用 respond_to 。在Rails中,我们可以通过两种方式呈现数据:


  1. Javascript


  2. HTML


我们将使用 javascript 呈现代码,因为它是借助 ID 删除< td> 标记的简便方法。 / p>

现在我们需要一个 js 文件来呈现 destroy 方法。为此,将 destroy.js.erb 添加到视图的同一文件夹中。



destroy.js.erb 中添加以下代码:

  var element = document.getElementById(<%= @ post.id%>)); 
element.parentNode.parentNode.remove();

您完成了!
我建议您阅读: http://www.gotealeaf.com/blog/the-detailed-guide-on-how-ajax-works-with-ruby-on-rails


I can easily delete the entries of post synchronously in rails application by following code:

views

<% @post.each do |post| %>
 <tr>
   <td colspan="3"><%= link_to 'Destroy', post, :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %></td>
 </tr>
<% end %>

Controller class

def destroy
 @post = Post.find(params[:id])
 @post.destroy
 respond_to do |format|
   format.html redirect_to(posts_url) }
   format.xml  { head :ok }
 end
end

This works fine. How make this destroy process async. So, that I will not required to add route or refresh the page.

Edit

Adding more explanation

Task is to make destroy method async. So that the client will not wait for the response and destroy method will run in background async. Also the destroy method has no guaranteed completion time.

Another example could be: I have to delete multiple post at a time.

解决方案

For making Async request you need Ajax and basic javascript to remove the code of <td>.

First change your views code to :

<% @post.each do |post| %>
<tr>
  <td colspan="3"><%= link_to 'Destroy', post, :method=> :delete, id: post.id, :class => 'btn btn-mini btn-danger', data: { confirm: 'Are you Sure?', remote: true } %> </td>
</tr>
<% end %>

Above code says that it will call the destroy method of PostController class. Now you need to make some changes in destroy method of controller class

def destroy
 @post = Post.find(params[:id])
 @post.destroy
 respond_to do |format|
   format.js 
   format.html { redirect_to(posts_url) }
 end
end

Above code is pretty understandable, except why we use respond_to. In rails we can render the data in two ways:

  1. Javascript

  2. HTML

we will use javascript to render the code because it's a easy way to remove the <td> tag with the help of ID.

Now we need a js file to render the response of destroy method. To do this add destroy.js.erb in the same folder of your views.

Add this following code in destroy.js.erb:

 var element = document.getElementById("<%= @post.id %>");
 element.parentNode.parentNode.remove();

You are done! I will recommend you to read : http://www.gotealeaf.com/blog/the-detailed-guide-on-how-ajax-works-with-ruby-on-rails

这篇关于如何在Rails中异步执行Controller.destroy操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆