【发布时间】:2014-07-01 22:48:28
【问题描述】:
我正在尝试在我的应用程序中启用 ajax 评论,并且我能够提交所有内容,但是当我尝试呈现 cmets 列表时,它呈现为空白。有谁知道我该如何解决这个问题?提前致谢。
cmets_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
statuses/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 %>
shared/_cmets.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>
cmets/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")
【问题讨论】:
-
在您的浏览器控制台中是否显示正在发送 ajax 请求?如果它已发送,那么它是否给出任何错误?
-
是的,请求正在发送并且没有错误评论区看起来正在呈现,但实际上没有出现
-
我想我知道出了什么问题,但不知道我需要做什么来纠正这个问题。
@comments在我的statuses_controller的 show action 中定义,但未在我的comments_controller创建调用的来源中定义。我需要做什么才能完成这项工作? -
您需要在创建操作中包含@cmets,因为那是您调用 create.js.erb 文件的位置
-
是的,就是这样。但现在奇怪的事情正在发生。部分没有更新。它停止渲染空白,但现在它根本不渲染,但如果我检查我的浏览器控制台,它说它正在渲染我想要的。
标签: jquery ruby-on-rails ajax acts-as-commentable