【问题标题】:How to perform Controller.destroy action asynchronously in rails?如何在 Rails 中异步执行 Controller.destroy 操作?
【发布时间】:2015-06-30 04:46:43
【问题描述】:

我可以通过以下代码轻松删除rails应用程序中postsynchronously的条目:

观看次数

<% @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 %>

控制器类

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

这很好用。如何使destroy 处理async。所以,我不需要添加route 或refresh the page。

编辑

添加更多解释

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

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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 controller


    【解决方案1】:

    尝试remote: true 选项,

    <%= link_to 'Destroy', post, :method => :delete, remote: true, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
    
    def destroy
      @post = Post.find(params[:id])
      @post.destroy
    
      # Just Guessing you have @posts to refresh and `posts_template` using it.
      @posts = Post.all 
    
      respond_to do |format|
        format.html redirect_to(posts_url) }
        format.xml  { head :ok }
        format.js { render 'posts.js.erb' }
      end
    end
    

    posts.js.erb

    $('some_element').replaceWith('<%=j render 'posts_template' %>');
    

    请根据需要进行更改。我只是猜测。

    【讨论】:

    • 我不需要对controller class 进行任何更改,因为它在提交请求后会重定向到一个页面吗?
    • 是的,在 respond_to 块中添加 format.js {} 块与 xml 或 html 相同。在其中添加您的更改。
    • 我有点困惑,因为我们没有将任何参数传递给 format.js {}. Please have a look at http://pastebin.com/tuiXce8f Also I tried and it's working. But after deleting the element page is not refreshing. This is the proper way to run Asynchronous` 用于 destroy 方法,还是只是一个调整?
    • 您必须在format.js {} 块中添加一个代码,请求完成后您需要更改的内容。就像您可以渲染任何 js 部分、任何 javascript 等...
    • 我做到了。这是正确的方法吗?因为clinet还在等待响应
    【解决方案2】:

    要发出Async 请求,您需要Ajax 和基本javascript 以删除&lt;td&gt; 的代码。

    首先将您的views 代码更改为:

    <% @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 %>
    

    上面的代码说它会调用PostController类的destroy方法。现在您需要对controller 类的destroy 方法进行一些更改

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

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

    1. Javascript

    2. HTML

    我们将使用javascript 来呈现代码,因为这是在ID 的帮助下删除&lt;td&gt; 标记的简单方法。

    现在我们需要一个js 文件来呈现destroy 方法的响应。为此,请在 views 的同一文件夹中添加 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

    【讨论】:

    • 感谢您的解释! :)
    猜你喜欢
    • 2014-04-10
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 2017-06-23
    • 2019-05-27
    • 1970-01-01
    相关资源
    最近更新 更多