(1) First add an attribute remote:true to the delete button so that ujs the click action into an AJAX call,
App/views/products/_product.html.erb
<tr id="product_<%= product.id%>"> <td><%= link_to product.id, Product_path (product)%></td> <TD id="product_<%= product.id%>_name"><%= product.name%></td> <td id="product_<%= product.id%>_price"><%= number_to_currency Product.price, Unit:"¥"%></td> <TD id="product_<%= product.id%>_description"><%= product.description%></td> <td><%=l product.created_at%></td> <td> < %= link_to T ('. Edit',:d efault = t ("Helpers.links.edit") ), Edit_product_path (product),:class='btn Btn-default btn-xs editproductlink', Remote:true, data: {type:'JSON'}%> <%= link_to t ('. Destroy',:d efault= = T ("Helpers.links.destroy") , product,: remote=true,: Method=:d elete,:d ata= = {: Confirm = + t ('. Confirm',:d efault = t ("helpers.links.confirm",:d efault ='Is you sure?')) }, :class='btn Btn-xs Btn-danger'%> </td></tr>
(2) in the controller's Destory method, add the request code to receive JS
def destroy @product. Destroy |format| ' Product was successfully destroyed. ' } Format.json {head:no_content} format.js end End
(3) Add File App/views/products/destroy.js.erb, using the Fadeout method, will be gradient to hide this line.
$ ('#product_ <%= @product. ID%>'). FadeOut ();
Delete function via JS Ruby on Rails