在我的应用程序中,我有一个评论系统,将评论显示为集合,我有ajax评论工作,但目前它再次呈现整个集合,但我想要的是只是添加最新评论。这是我的代码:
comments_controller.rb
class CommentsController < ApplicationController
before_filter :authenticate_member!
before_filter :load_commentable
before_filter :find_member
def index
redirect_to root_path
end
def new
@comment = @commentable.comments.new
end
def create
@comment = @commentable.comments.new(params[:comment])
@comment.member = current_member
respond_to do |format|
if @comment.save
format.html { redirect_to :back }
format.json
format.js
else
format.html { redirect_to :back }
format.json
format.js
end
end
end
def destroy
@comment = Comment.find(params[:id])
respond_to do |format|
if @comment.member == current_member || @commentable.member == current_member
@comment.destroy
format.html { redirect_to :back }
else
format.html { redirect_to :back, alert: 'You can\'t delete this comment.' }
end
end
end
private
def load_commentable
klass = [Status, Medium, Project, Event, Listing].detect { |c| params["#{c.name.underscore}_id"] }
@commentable = klass.find(params["#{klass.name.underscore}_id"])
end
def find_member
@member = Member.find_by_user_name(params[:user_name])
end
end
statuses_controller
def show
@status = Status.find(params[:id])
@commentable = @status
@comments = @commentable.comments.order('created_at desc').page(params[:page]).per_page(15)
@comment = Comment.new
respond_to do |format|
format.html # show.html.erb
format.json { redirect_to profile_path(current_member) }
format.js
end
end
状态/ show.html.erb
<% if member_signed_in? %>
<div id="comm_form_wrap">
<%= render "shared/comment_form" %>
</div>
<div id="comments_<%= @commentable.id %>">
<%= render partial: "shared/comments", :collection => @comments, :as => :comment %>
</div>
<% end %>
共享/ _comments.html.erb
<div id="comment_<%= comment.commentable.id %>_<%= comment.id %>" class="comments">
<span class="comment_av">
<%= comment_avatar_link_2 comment.member, :class => "com_av", title: comment.member.full_name, alt: comment.member.full_name %>
</span>
<span>
<div class="comment_name">
<% if comment.member == @commentable.member %>
<%= link_to comment.member.user_name, profile_path(comment.member) %> <span class="owner">Creator</span>
<% else %>
<%= link_to comment.member.user_name, profile_path(comment.member) %>
<% end %>
<% if comment.member == current_member || @commentable.member == current_member %>
<span class="comment_del">
<%= link_to image_tag("Delete.png", title: 'Delete'), [@commentable, comment], remote: true, method: :delete, data: { confirm: 'Are you sure?' } %>
</span>
<% end %>
<span class="meta">
<%= time_ago_in_words(comment.created_at) %>
</span>
</div>
<div class="com_con">
<%= Rinku.auto_link(comment.content).html_safe %>
</div>
</span>
</div>
评论/ create.js.erb
$("#comments_<%= @commentable.id %>").html("<%= escape_javascript(render :partial => 'shared/comments', :collection => @comments, :as => :comment) %>");
$('#comment_form')[0].reset();
如果我只是在html
文件中将append
更改为create.js.erb
,则会附加整个评论集。我想我可以复制并粘贴我的评论中的代码部分,但这将是很多代码,我不知道如何定义变量。添加单个新评论的最佳方式是什么?提前谢谢。
答案 0 :(得分:0)
我能够解决这个问题。我可以像这样附加@comment
而不是渲染或追加集合:
$("#comments_<%= @commentable.id %>").append("<%= escape_javascript(render :partial => @comment, :locals => {:comment => @comment}) %>");
然后我只需要在我的评论文件夹中创建一个_comment.html.erb
部分。