我正在尝试在我的应用中启用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_wrap 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">
<div class="com_con">
<%= Rinku.auto_link(comment.content).html_safe %>
</div>
</div>
评论/ create.js.erb
$("#comm_form_wrap").html("<%= escape_javascript(render :partial => 'shared/comment_form') %>");
$('#comment_box').val('');
$("#comments_<%= @commentable.id %>").html("<%= escape_javascript(render :partial => 'shared/comments', :collection => @comments, :as => :comment) %>")
当我检查浏览器控制台时,它显示了这种情况:
$("#comm_form_wrap").html("<%= escape_javascript(render :partial => 'shared/comment_form') %>");
$('#comment_box').val('');
$("#comments_<%= @commentable.id %>").html("")
**编辑**
$("#comments_93").html("/n <\/div>\n\n <div class=\"com_con\">\n testing\n <\/div>\n <\/div>\n\n")
答案 0 :(得分:0)
create.js.erb
中的最后一行是指HTML页面中不存在的id
。将其更改为#comments_wrap
,它就会变得更好。
答案 1 :(得分:0)
如果有其他人遇到这个问题我通过在控制器中定义@comments
变量来解决这个问题,该变量创建了操作并调用了ajax。