通过Ajax进行Rails远程删除和更新视图 [英] Rails remote delete and update view through Ajax

查看:92
本文介绍了通过Ajax进行Rails远程删除和更新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去,每当我想通过Ajax更新视图的一部分时,我都会做以下事情:

In the past, whenever I wanted to update a part of my view through Ajax, I've done the following:

  1. 在要更新的零件上创建部分零件,并为其指定一个唯一的ID,例如 #tracks
  2. 在控制器中为该Ajax调用创建一个特殊操作,例如说 remove_track 来更新所有值,等等,并添加 format.js
  3. 创建一个与动作名称相同的新JS文件,以便Rails自动将其调用 remove_track.js.erb ,其中包含以下内容: $('#tracks').html(<%= j渲染'cds/show_tracks'%>"));
  4. 在调用此操作的链接中
  5. 设置 remote:true .
  1. create a partial out of the part I want to update and give it a unique ID, say #tracks
  2. create a special action in the controller for that Ajax call, say remove_track that updates all the values, etc. and add format.js
  3. create a new JS file with the same name as the action so Rails calls it automatically remove_track.js.erb which contains something like: $('#tracks').html("<%=j render 'cds/show_tracks' %>");
  4. set remote: true in the link that calls this action.

这一切都很好,但是现在我尝试使用常规的 destroy 方法来删除和更新常见的 index 视图以提高灵活性,这意味着我可以调用该方法通过Ajax或正常情况下.我认为这是很平常的事情,必须有比上述所有方法更好的方法.

All this is fine, but now I am trying to delete and update a common index view using the regular destroy method for flexibility, meaning I can call this method either through Ajax or normally. I figured it's such a common thing to do that there has to be a better way than all of the above.

只需将其放入控制器中,便可以获取destroy方法来调用我的 destroy.js.erb 文件:

I can get the destroy method to call my destroy.js.erb file by simply putting this into the controller:

  format.js { layout: false }

,当然还要在链接上设置 remote:true .

and of course setting remote: true on the link.

我不能做的就是刷新视图.我要刷新的表包含在具有唯一ID的div中,但是由于它不是局部表,因此它拒绝刷新内容.也许我想念一些东西.

what I cannot do is get the view to refresh. The table I want to refresh is encased in a div with a unique ID, but since it's not a partial, it refuses to refresh the content. Maybe I'm missing something.

我注定必须使用上述方法创建部分内容并刷新它,还是有一种更神奇的方法(除了使用Turbolinks之外)?

Am I doomed to have to create a partial and refresh it with the method above or is there a more magical way of doing it (other than using Turbolinks)?

谢谢.

PS 此外,我刚刚注意到这还有一个缺点,即我无法将其余参数传递给destroy方法,因为它仅传递对象ID来使用常规CRUD路由销毁.如果我尝试使用 platform(action:destroy) platform(method:delete),则会收到错误消息:

PS Also, I just noticed this has the added disadvantage that I cannot pass the rest of the params to the destroy method since it only passes the object ID to destroy using the regular CRUD routes. If I try to use platform(action: destroy) or platform(method: delete) I get an error:

No route matches {:action=>"destroy", :controller=>"platforms"}

这意味着如果我想传递这些参数,我必须创建一条新路线...

Which means I have to create a new route if I want to pass those parameters...

所有这些的另一个缺点是,我不再使用destroy方法中的index方法中的搜索和排序逻辑.我敢肯定,这绝对不是.

Yet another disadvantage to all this is that I'm repeading all the logic for searches and sorting that I have in the index method again in the destroy method. I am certain this is definitely not the way to do it.

推荐答案

通过此页面,我找到了正确的方法.如此简单有效.

Thanks to this page I found the proper way to do it. So simple and effective.

http://carmennorahgraydean.blogspot.com.es/2012/10/rails-328-ajax-super-basic-example.html

更新index.html.erb中的销毁线:

Update your destroy line in index.html.erb:

<%= link_to 'Destroy', pony, method: :delete, data: { confirm:
'Are you sure?' }, :remote => true, :class => 'delete_pony' %>

创建一个文件destroy.js.erb,并将其放在其他.erb文件旁边(在应用程序/视图/小马下).它应该看起来像这样:

Create a file, destroy.js.erb, put it next to your other .erb files (under app/views/ponies). It should look like this:

$('.delete_pony').bind('ajax:success', function() {     
  $(this).closest('tr').fadeOut();
});

将format.js {render:layout => false}添加到您的控制器中:

Add format.js { render :layout => false } to your controller:

respond_to do |format|
 format.html { redirect_to ponies_url }
 format.json { head :no_content }
 format.js   { render :layout => false }
end

希望这对其他人有帮助.

Hope this helps someone else.

这篇关于通过Ajax进行Rails远程删除和更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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