微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – ajax最近在rails上的ruby中不受影响

你好我是 ruby on rails上的新手.我尝试在我的演示网站上做一些ajax.Delete已执行但我必须每次刷新.

视图/共享/ _Feed_item.html.haml

%li{id: Feed_item.id}
= image_tag current_user.image_url(:small_thumb).to_s
%span.user
%span.destination
%b Source:
= Feed_item.source
%span.destination
%b Destination:
= Feed_item.destination
%span.destination
%b Date:
= Feed_item.date
%span.timestamp
%b Time:
= Feed_item.time
%span.content
%b Comment:
= Feed_item.content
%span.timestamp
- if current_user == Feed_item.user
= link_to "Delete",Feed_item,method: :delete,data: { confirm: "You sure?" },title:  "Delete",remote: true,class: "delete_post"

查看/请求/ delete.js.haml

$(document).ready(function(){
  $(function(){
    $('.delete_post').bind('ajax:success',function() {
      $(this).closest('li').fadeOut();
    });
  });
});

控制器/ requests_controller.rb

def destroy
 @request.destroy
 respond_to do |format|
   format.html { redirect_to root_url,notice: "Post deleted" }
   format.js { render nothing: true}
 end
end

原始的HTML链接

link

提前致谢.

解决方法

之所以jquery不起作用.问题是turbolinks. $(document).ready(function()不使用turbolinks.更多看看这个 description

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐