Ruby on rails 带有Rails的嵌套注释的祖先Gem导致未定义的方法错误

Ruby on rails 带有Rails的嵌套注释的祖先Gem导致未定义的方法错误,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我一直在尝试修复一个与在我的Rails 4应用程序上使用祖先宝石进行评论相关的错误。我曾经当过导游。但是,与本集不同的是,我的注释模型是另一个模型中的嵌套资源。在我继续之前,我将提供必要的代码供参考。如果您想立即阅读错误,那么在所有代码片段之后都会提到它 相关模型: class Comment < ActiveRecord::Base has_ancestry belongs_to :user belongs_to :scoreboard end class Scor

我一直在尝试修复一个与在我的Rails 4应用程序上使用祖先宝石进行评论相关的错误。我曾经当过导游。但是,与本集不同的是,我的注释模型是另一个模型中的嵌套资源。在我继续之前,我将提供必要的代码供参考。如果您想立即阅读错误,那么在所有代码片段之后都会提到它

相关模型:

class Comment < ActiveRecord::Base
  has_ancestry 
  belongs_to :user
  belongs_to :scoreboard
end



 class Scoreboard < ActiveRecord::Base 
  #scoreboard model is like an article page on which users can post comments
  belongs_to :user
  has_many :teams, dependent: :destroy
  has_many :comments, dependent: :destroy
  end
可发表评论的页面的记分板控制器方法:

def show
  @scoreboard = Scoreboard.find_by_id(params[:id])
  @team = @scoreboard.teams.build
  @comment = @scoreboard.comments.new
 end
def nested_comments(comments)
  comments.map do |comment, sub_comment| #the comments.map also gives me an error if I choose to render the comments without the .arrange ancestry method 
    render(comment) + content_tag(:div, nested_comments(sub_comment), class:  "nested_messages")
  end.join.html_safe
end
财务主任:

class CommentsController < ApplicationController

    def new
     @scoreboard = Scoreboard.find(params[:scoreboard_id])
     @comment = @scoreboard.comments.new(:parent_id => params[:parent_id])
    end


    def create
     @scoreboard = Scoreboard.find(params[:scoreboard_id])
     @comment = @scoreboard.comments.new comment_params
     if @comment.save
         redirect_to scoreboard_url(@comment.scoreboard_id) 
     else
       render 'new'
     end
    end

 private

     def comment_params
      params.require(:comment).permit(:body, :parent_id).merge(user_id: current_user.id)
     end

end
new.html.erb用于回复表单提交时重定向到的注释:

<%= form_for [@scoreboard, @comment] do |f| %> 
        <%= render 'shared/error_messages', object: f.object %>
        <%= f.text_area :body, class: "comment-field" %>
        <%= f.hidden_field :parent_id %>   
        <%= f.submit "Join the discussion...", class: " comment-button btn btn-primary" %>               
<% end %>

在创建记分板时,我被重定向到show页面,在那里我得到以下错误:

[]的未定义方法“arrange”:数组

即使注释数组是空的,如果不是,我也会得到同样的错误。我尝试了.subtree.arrange,但这也给了我同样的错误。另外,祖先文档中说.arrange只在作用域类上工作。我不知道那是什么意思。我将感谢一些帮助,使网页的工作,使评论显示正确的顺序与回复后,他们的父母的意见。如果这是线程评论(回复和全部)的错误方法,我将感谢您对下一步研究内容的指导。

。拒绝(&:new\u record?
这将返回一个数组。这个错误听起来像是ActiveRecord上的一个作用域。因此,将
拒绝
移动到末尾,它应该可以工作

@scoreboard.comments.arrange(:order => :created_at).reject(&:new_record?)
.reject(&:new_record?)
这将返回一个数组。这个错误听起来像是ActiveRecord上的一个作用域。因此,将
拒绝
移动到末尾,它应该可以工作

@scoreboard.comments.arrange(:order => :created_at).reject(&:new_record?)

关于您的注释嵌套,我以前已经实现过,并且发现Railscasts推荐的助手非常弱

相反,您最好使用一个分部,该分部根据每个
注释的子项数递归:

#app/views/scoreboards/show.html.erb
<%= render @comments %>

#app/views/scoreboards/_comment.html.erb
<%= link_to comment.title, comment_path(comment) %>
<div class="nested">
    <%= render comment.children if comment.has_children? %>
</div>
#app/views/scoreboards/show.html.erb
#app/views/scoreboards/_comment.html.erb

关于您的注释嵌套,我之前已经实现了这一点,并且发现Railscasts推荐的助手非常弱

相反,您最好使用一个分部,该分部根据每个
注释的子项数递归:

#app/views/scoreboards/show.html.erb
<%= render @comments %>

#app/views/scoreboards/_comment.html.erb
<%= link_to comment.title, comment_path(comment) %>
<div class="nested">
    <%= render comment.children if comment.has_children? %>
</div>
#app/views/scoreboards/show.html.erb
#app/views/scoreboards/_comment.html.erb

4天来,我一直在试图找出这个错误。你5分钟就解决了。非常感谢你!4天来,我一直在试图找出这个错误。你5分钟就解决了。非常感谢你!我实际上在想如何摆脱那个助手,现在给你一个答案。我希望我能接受两个答案。另外,如果您不介意的话,您能否解释一下下面这行代码是如何工作的:哦,对不起,这行代码只是一个演示,可以在您的部分代码中使用
注释
数据。你可以忽略它;否则你应该在这里阅读如何呈现收藏:酷,谢谢!我也喜欢在另一篇文章上嵌套的下拉列表。很酷的东西。非常感谢你的帮助。没问题,希望你一切顺利。我实际上在想如何摆脱这个帮手,给你一个答案。我希望我能接受两个答案。另外,如果您不介意的话,您能否解释一下下面这行代码是如何工作的:哦,对不起,这行代码只是一个演示,可以在您的部分代码中使用
注释
数据。你可以忽略它;否则你应该在这里阅读如何呈现收藏:酷,谢谢!我也喜欢在另一篇文章上嵌套的下拉列表。很酷的东西。非常感谢你的帮助。没问题,希望一切顺利
@scoreboard.comments.arrange(:order => :created_at).reject(&:new_record?)
#app/views/scoreboards/show.html.erb
<%= render @comments %>

#app/views/scoreboards/_comment.html.erb
<%= link_to comment.title, comment_path(comment) %>
<div class="nested">
    <%= render comment.children if comment.has_children? %>
</div>