Rails remote delete and update view through Ajax

北战南征 提交于 2019-12-03 03:32:45
kakubei

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

Update your destroy line in index.html.erb:

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

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();
});

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!