简体   繁体   English

我如何使用 jquery 呈现部分?

[英]How can i render partial with jquery?

I try to add AJAX to my blog.我尝试将 AJAX 添加到我的博客中。 And i want to make post, edit and delete comments using ajax.我想使用 ajax 发布、编辑和删除评论。 For example, to post a comment, I used:例如,要发表评论,我使用了:

$(".comment<%=@comment.parent_id %>").append('<%= j render partial:'comments/comment', locals: {comment: @comment} %>');

But due to the fact that I have nesting, it was not displayed as I would like.但是由于我有嵌套的事实,它没有按照我想要的方式显示。 All comments I have are in _comment.html.erb.我的所有评论都在 _comment.html.erb 中。 Can I render a partial without reloading the page using jquery?我可以在不使用 jquery 重新加载页面的情况下呈现部分吗? And is this the right approach?这是正确的方法吗?

As I already wrote, all the comments I had were in the partial.正如我已经写过的,我所有的评论都是部分的。 And because of the nesting, I could not just add a new comment using the append function.而且由于嵌套,我不能只使用append函数添加新评论。 Therefore, I just update the partial on the page, without reloading the page as follows:因此,我只更新页面上的部分,而不重新加载页面,如下所示:

$(".comments").replaceWith("<div class='comments'><%= j nested_comments @post.comments.arrange(order: :created_at)%>")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM