博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
通过js实现删除功能 ruby on rails
阅读量:7052 次
发布时间:2019-06-28

本文共 1192 字,大约阅读时间需要 3 分钟。

(1)首先给删除按钮增加一个属性 remote: true,这样ujs将这个click动作转化为ajax调用,

 app/views/products/_product.html.erb

  <%= link_to product.id, product_path(product) %>  <%= product.name %>  <%= number_to_currency product.price, unit: "¥" %>  <%= product.description %>  <%=l product.created_at %>      <%= link_to t('.edit', :default => t("helpers.links.edit")),      edit_product_path(product), :class => 'btn btn-default btn-xs editProductLink',      remote: true, data: {type: 'json'} %>      <%= link_to t('.destroy',        :default => t("helpers.links.destroy")),        product,        :remote => true,        :method => :delete,        :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },        :class => 'btn btn-xs btn-danger' %>  

(2)在controller的destory方法里,增加接收js的请求代码

def destroy    @product.destroy    respond_to do |format|      format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }      format.json { head :no_content }      format.js    end  end

(3)新增文件app/views/products/destroy.js.erb,用的是fadeout方法,会渐变的将这一行隐藏掉。

$('#product_<%= @product.id %>').fadeOut();

 

转载于:https://www.cnblogs.com/iwangzheng/p/4915113.html

你可能感兴趣的文章
nodejs中安装express
查看>>
2014软件表
查看>>
Struts2教程3:struts.xml常用配置解析
查看>>
(转帖)Implementing custom JavaFx Bindings
查看>>
mysql外键
查看>>
转发和重定向的区别
查看>>
<ecmall> ECMall的MySQL数据库调用
查看>>
SugarCRM - 如何修改默认首页
查看>>
Java日期类总结
查看>>
开关标识一个是0一个是-那个是开哪个是关
查看>>
ruby的并发和并行
查看>>
朱晶晶-六步制定好企业移动化战略
查看>>
SVPullToRefresh
查看>>
SSIndicatorLabel
查看>>
ASFBPostController
查看>>
Android实战技巧:Handler
查看>>
JqueryMobile实践点滴
查看>>
teamtalk服务端之完美一键部署脚本(ubuntu)
查看>>
2014.7.26 为cocos2d-x3.2版本增加protobuffer2.5.0支持
查看>>
Java进阶篇设计模式之一 ----- 单例模式
查看>>