【问题标题】:Rails ajax render partial returning blankRails ajax 渲染部分返回空白
【发布时间】: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


【解决方案1】:

create.js.erb 中的最后一行是指 HTML 页面中不存在的 id。将其更改为#comments_wrap,一切都会好起来的。

【讨论】:

  • 抱歉忘记在我的编辑中更改它,但 div 确实有那个 id。我也将@cmets 添加到我的 cmets 控制器中,因此它不再返回空白。它根本没有做任何事情。没有错误,调试器显示新注释,但没有进行实际渲染。
  • 我看到您将 id 添加到 div 中。但看起来你有两个 id 之间有一个空格。如果是这样的话,它就行不通了。从 id 和空间中删除 cmets_wrap 部分。
  • 是的,很抱歉,但我发现只是忘记在此处更改了。现在一切正常。
【解决方案2】:

如果其他人遇到这个问题,我通过在创建动作并调用 ajax 的控制器中定义 @comments 变量来解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-30
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多