[英]Rails ajax render partial returning blank
I'm trying to enable ajax commenting in my app and I'm able to submit everything just fine but when I try to render the list of comments it renders blank. 我正在尝试在我的应用程序中启用Ajax评论,并且能够提交所有内容,但是当我尝试呈现评论列表时,它将呈现空白。 Does anyone know how I can fix this? 有谁知道我该如何解决? Thanks in advance. 提前致谢。
comments_controller.rb 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 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 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/_comments.html.erb shared / _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>
comments/create.js.erb 评论/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) %>")
When I inspect the browser console it shows this happening: 当我检查浏览器控制台时,它显示了这种情况:
$("#comm_form_wrap").html("<%= escape_javascript(render :partial => 'shared/comment_form') %>");
$('#comment_box').val('');
$("#comments_<%= @commentable.id %>").html("")
**EDIT** **编辑**
$("#comments_93").html("/n <\/div>\n\n <div class=\"com_con\">\n testing\n <\/div>\n <\/div>\n\n")
The last line in your create.js.erb
is referring to an id
that doesn't exist in your HTML page. create.js.erb
的最后一行是指HTML页面中不存在的id
。 Change it to #comments_wrap
and it'll be all good. 将其更改为#comments_wrap
,一切都会很好。
万一其他人遇到此问题,我通过在创建动作并调用ajax的控制器中定义@comments
变量来解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.