【发布时间】:2014-04-22 20:56:16
【问题描述】:
您好,我是 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 链接
提前致谢。
【问题讨论】:
-
您的实际问题是什么?当前代码的期望行为和观察到的行为是什么?
-
我想在不刷新的情况下淡出帖子(通过 ajax)。所以当前代码显示成功删除但不是通过 ajax 。
-
如果您显示生成的 HTML(浏览器和 jQuery 看到的内容)而不是 html.haml 源代码,将会有更多人能够帮助您。就像现在一样,任何不知道如何阅读您的 haml 文件的人都很难为您提供帮助,但如果您只是公开实际的 HTML,则不需要这些知识来帮助您调试 jQuery。
标签: javascript jquery ajax ruby-on-rails-4 turbolinks