我只是嘗試了很多,並尋找答案,但沒有發現任何東西.... 我有一個帖子的意見,用戶可以編輯他們的意見.. 如果他點擊在評論中編輯,表單將顯示在評論的位置......所有這些都在工作,但是當您提交更新時,評論將會在同一篇文章中的評論列表下方,編輯表單將保留如果他再次提交一份(公佈的),則會在列表下方顯示...現在,如果我刷新頁面,更新的評論將返回到正確的位置,並且已公佈的一個將被刪除... 如何解決?我想用更新commetns來代替,而無需刷新替換形式...提交編輯評論在同一個地方與AJAX,jQuery的鐵軌
這裏是我的帖子頁腳在評論desiplayed:
<div class="panel-footer" id="panel_footer_<%= post.id %>">
<div class="comment-form" id="comment-form-j">
<%= render 'comment2', post: post, comment: post.comments.build %>
</div>
<div class="comments" id= "comments_<%= post.id %>">
<% if post.comments.present? %>
<%= render post.comments, post: post %>
<% end %>
</div>
</div>
這裏是我的評論部分_comment.html.erb:
<% if comment.user_id.present? %>
<div id="current_comment_<%= comment.id %>" >
<div id="comment">
<div id="avatar_comment_<%= comment.user_id %>">
<%= link_to image_tag(comment.user.avatar.url, size: "30x30", class: "img-circle img-comments"), profile_path(comment.user.user_name) %>
</div>
<div class="user-name">
<%= link_to comment.user.user_name, profile_path(comment.user.user_name), class: "username-size" %>
</div>
<div class="comment-content comment-with-menu menu-comment-line" id = "this_comment_<%= comment.id %>" >
<%= comment.content %>
</div>
<% if comment.user == current_user %>
<ul class="comment-menuu menu-comment-line">
<li class="dropdown">
<a href="#" class="dropdown-toggle menu-right-comment " data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"> <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><%= link_to "Edit", edit_post_comment_path(post, comment), remote: true %></li>
<li role="separator" class="divider"></li>
<li><%= link_to "delete", post_comment_path(post, comment), method: :delete, data: { confirm: "Are you sure?" }, remote: true %></li>
</ul>
</li>
</ul>
<% end %>
</div>
</div>
<% end %>
這裏是員額我的評論的形式(_comment2.html.erb):
<%= form_for([post, comment], remote: true) do |f| %>
<%= f.text_field :content, placeholder: 'Add a comment...', class: "form-control comment_content", id: "comment_content_#{post.id}" %>
<% end %>
這是我下評論edit.js.erb:
$("#this_comment_<%= @comment.id %>").replaceWith("<%= j render 'posts/comment2', post: @post, comment: @comment %>");
這裏是我的下評論update.js.erb:
$('#comments_<%= @post.id %>').append("<%=j render 'comments/comment', post: @post, comment: @comment, remote: true %>");
,這裏是註釋控制器,其中編輯和更新:
def edit
@comment = @post.comments.find(params[:id])
respond_to do |format|
format.js
end
end
def update
@comment = @post.comments.find(params[:id])
if @comment.user_id == current_user.id
if @comment.update(comments_params)
respond_to do |format|
format.html {redirect_to root_path}
format.js
end
else
flash[:alert] = "Something worng, try again"
render root_path
end
end
end
不,我不想在前面加上...我要評論再次顯示,而不是形式更新的內容...所以想要隱藏形式,然後顯示更新的內容而不重定向頁面 – sam0101
在這種情況下,您可以使用jQuery的replace()將新的註釋替換爲表單標籤。或者你將表單封裝在一個div中,然後使用'.html(「<%= j render .....%>」)' –
謝謝...這個.html幫助了我...我這樣做$( '#comments _ <%= @ post.id%>')。html(「<%= j render @ post.comments,post:@ post,comment:@ comment%>」);我在那裏提交所有的意見後提交..這是很好的渲染所有的意見都有更好的辦法? – sam0101