gpt4 book ai didi

javascript - ajax closest 在 ruby​​ on rails 中不受影响

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:33:51 25 4
gpt4 key购买 nike

你好,我是 ruby​​ on rails 的新手。我尝试在我的演示网站上做一些 ajax。删除已执行,但我每次都必须刷新。

查看/共享/_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"

查看/requests/delete.js.haml

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

controller/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 不工作的原因。问题是涡轮链接。 $(document).ready(function() 不适用于 turbolinks。更多信息请查看 description

关于javascript - ajax closest 在 ruby​​ on rails 中不受影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23230034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com