Jquery Rails ajax渲染部分返回空白
我正在尝试在我的应用程序中启用ajax评论,我可以提交所有内容,但当我尝试呈现评论列表时,它会呈现为空白。有人知道我怎么解决这个问题吗?提前谢谢 评论\u controller.rbJquery Rails ajax渲染部分返回空白,jquery,ruby-on-rails,ajax,acts-as-commentable,Jquery,Ruby On Rails,Ajax,Acts As Commentable,我正在尝试在我的应用程序中启用ajax评论,我可以提交所有内容,但当我尝试呈现评论列表时,它会呈现为空白。有人知道我怎么解决这个问题吗?提前谢谢 评论\u controller.rb class CommentsController < ApplicationController before_filter :authenticate_member! before_filter :load_commentable before_filter :find_member def index
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
<% 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 %>
<div id="comment_<%= comment.commentable.id %>_<%= comment.id %>" class="comments">
<div class="com_con">
<%= Rinku.auto_link(comment.content).html_safe %>
</div>
</div>
$("#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) %>")
状态/show.html.erb
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
<% 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 %>
<div id="comment_<%= comment.commentable.id %>_<%= comment.id %>" class="comments">
<div class="com_con">
<%= Rinku.auto_link(comment.content).html_safe %>
</div>
</div>
$("#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) %>")
@评论,:as=>:评论%>
shared/_comments.html.erb
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
<% 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 %>
<div id="comment_<%= comment.commentable.id %>_<%= comment.id %>" class="comments">
<div class="com_con">
<%= Rinku.auto_link(comment.content).html_safe %>
</div>
</div>
$("#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) %>")
评论/create.js.erb
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
<% 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 %>
<div id="comment_<%= comment.commentable.id %>_<%= comment.id %>" class="comments">
<div class="com_con">
<%= Rinku.auto_link(comment.content).html_safe %>
</div>
</div>
$("#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(“'shared/comment_form')%>”;
$(“#注释框”).val(“”);
$(“#comments"”)html(“'shared/comments',:collection=>@comments,:as=>:comment)%>”)
当我检查浏览器控制台时,它显示了这种情况:
$("#comm_form_wrap").html("<%= escape_javascript(render :partial => 'shared/comment_form') %>");
$('#comment_box').val('');
$("#comments_<%= @commentable.id %>").html("")
$(“#comm_form_wrap”).html(“'shared/comment_form')%>”;
$(“#注释框”).val(“”);
$(“#注释”)html(“”)
**编辑**
$("#comments_93").html("/n <\/div>\n\n <div class=\"com_con\">\n testing\n <\/div>\n <\/div>\n\n")
$(“#comments_93”).html(“/n\n\n\n testing\n\n\n”)
您的create.js.erb
中的最后一行是指HTML页面中不存在的id
。把它改成#comments\u wrap
,一切都会好的 如果其他人遇到此问题,我通过在创建操作并调用ajax的控制器中定义@comments
变量来解决此问题。在浏览器控制台中是否显示正在发送ajax请求?如果它被发送了,那么它是否给出了任何错误?是的,请求被发送了,并且没有错误。注释区域看起来像是被呈现了,但实际上什么也没有出现。我想我知道哪里出了问题,但不知道我需要做些什么来纠正这个问题@comments
在mystatuses\u controller
的my show action中定义,但在创建调用的mycomments\u controller
中未定义。我需要做些什么才能让它工作呢?你需要在你的创建操作中有@comments,因为这就是你调用create.js.erb文件的地方。但现在有些奇怪的事情发生了。该部分未更新。它停止呈现空白,但现在它根本不呈现,但如果我检查浏览器控制台,它会显示它正在呈现我想要的内容。抱歉,忘记在编辑中更改它,但div确实具有该id。我还将@comments添加到我的comments控制器中,因此它不再返回空白。它什么也没做。没有错误,调试器显示了新的注释,但没有进行实际的渲染。我看到您将id添加到了div中。但是看起来您有两个id,中间有一个空格。如果是这样的话,那就行不通了。删除id和空间中的注释。\u wrap部分。是的,很抱歉,我发现只是忘了在这里更改它。现在一切正常。