【问题标题】:Threaded Comments - undefined method `children' for WillPaginateThreaded Comments - WillPaginate 的未定义方法“children”
【发布时间】:2011-04-09 23:55:32
【问题描述】:

下面的问题已经修复,我在cmets后面加了一个's',本来应该是comment,但是在这个帖子的底部又出现了一个新问题。

我正在尝试按照这个人的问题的答案为我已经存在的 cmets 系统获取线程化 cmets:Reddit-style nested/threaded/indented comments for Rails? 当我尝试访问我的文章显示页面时,我收到以下错误:undefined method `children' for # 这是我的文章显示页面中存在问题的视图:

<div id="comment <%= comment.id %>">
  <%= comment.title %>
  | <%= link_to "Edit Comment", edit_article_comment_path(@commentable, comment) %>
  | <%= link_to 'Delete Comment', [@commentable, comment], :confirm => "Are you sure?", :method => :delete %><br />
  <%= comment.content %><br />
  **<%= render :partial => 'comments/comment', :collection => @comments.children %>**
  <%= comment.user.name %><br /><br />
</div>

渲染部分有问题。 如果我从 cmets 中取出子部分,我会在控制台中得到这个: 递归永远不会停止。我明白了: 渲染 cmets/_form.html.erb (14.9ms) 渲染 cmets/_comment.html.erb (2.0ms) 渲染 cmets/_comment.html.erb (7.3ms) 渲染 cmets/_comment.html.erb (12.3 ms) 渲染 cmets/_comment.html.erb (17.9ms) 渲染 cmets/_comment.html.erb (22.3ms) 渲染 cmets/_comment.html.erb (26.8ms) 渲染 cmets/_comment.html.erb (31.6ms)渲染 cmets/_comment.html.erb (36.2ms) .... 我不知道为什么它一直在渲染。 没有一个 cmets 有孩子,所以它应该停止。 这是文章控制器的“显示”部分:

默认显示 @article = Article.find(params[:id]) @commentable = Article.find(params[:id]) @cmets = @commentable.cmets.paginate(:page => params[:page]) @comment = Comment.new @title = @article.title 结束

我唯一的解释是可能没有任何子 cmets,所以 will_paginate 不知道如何处理它,所以它会向我抛出错误。


新问题,我遇到了 cmets 路由问题。我正在为我的 cmets 使用多态关联,因此它们可以用于不同的模型(文章、配置文件、图片等),但我不知道如何在我的视图中创建路由路径。 这是我现在拥有的:

<div id="comment <%= comment.id %>">
  <%= comment.title %>
  | <%= link_to "Permalink", article_comment_path(@commentable, comment) %>
  | <%= link_to "Reply", new_article_comment_path(@commentable, @comment_child) %>
  | <%= link_to "Edit Comment", edit_article_comment_path(@commentable, comment) %>
  | <%= link_to 'Delete Comment', [@commentable, comment], :confirm => "Are you sure?", :method => :delete %><br />
  <%= comment.content %><br />
  <%= comment.user.name %><br /><br />
  <%= render :partial => 'comments/comment', :collection => @comment.children %>
</div>

存在大量不适用于其他模型的路径错误。我正在使用:article_comment_path、new_article_comment_path 和 edit_article_comment_path。顺便说一句,这是在一个 for each 循环中,它在一个 cmets 数组上循环。我想要“commentable_comment_path”或“new_commentable_comment_path”之类的东西,所以我可以将它用于我的其他模型。

还有一个简单的问题,这会修复我的“回复”链接吗?我不确定我这样做是否正确。我希望回复链接在评论中创建评论。

非常感谢。

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    parent_id 正如您所建议的,它是对对象的整数引用。要获取对象a 的子对象,您需要查找所有具有parent_ida.id 的对象。

    至于渲染的问题:@comments 是一个评论对象数组——你实际上是通过使用paginate 请求一页 cmets。我猜您想自己渲染 cmets,在这种情况下 :collection =&gt; @comments 将完成这项工作。这只是意味着对集合中的每个项目使用部分。然后,部分将负责根据链接的答案呈现每个评论的子项。

    【讨论】:

    • edit:哎呀,不知道enter发了回复。递归永远不会停止。我明白了: 渲染 cmets/_form.html.erb (14.9ms) 渲染 cmets/_comment.html.erb (2.0ms) 渲染 cmets/_comment.html.erb (7.3ms) 渲染 cmets/_comment.html.erb (12.3 ms) 渲染 cmets/_comment.html.erb (17.9ms) 渲染 cmets/_comment.html.erb (22.3ms) 渲染 cmets/_comment.html.erb (26.8ms) 渲染 cmets/_comment.html.erb (31.6ms)渲染 cmets/_comment.html.erb (36.2ms) .... 我不知道为什么它一直在渲染。没有一个 cmets 有孩子,所以它应该停止。
    • 啊,您是否将部分中的:collection =&gt; comments.children 替换为我的答案中的代码?我的意思是您的顶级视图中的代码。老实说,我很确定它也应该是部分的comment.children,而不是comments.children
    • 哈哈,是的,这是 cmets 中“s”的愚蠢错位。它应该只是“评论”。我忘记了,它在“针对 cme​​ts 中的每个评论”循环中。谢谢。另外,我还有一个问题,我将用相关信息编辑主帖。
    • 您最好提出一个新问题,然后再参考这个问题,而不是继续添加另一个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    相关资源
    最近更新 更多