2015-10-14 35 views
1

我一直在尝试解决与在我的应用程序Rails 4上使用祖先宝石相关的错误。我使用railscast episode 262作为指南。然而,与情节不同,我的评论模型是另一个模型中的嵌套资源。在我进一步研究之前,我将提供必要的代码供参考。如果您想立即阅读该错误,则会在所有代码片段之后立即提到。用于嵌套评论的祖先宝石导致未定义的方法错误

相关机型:

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 

在路由文件的相关代码:

resources :scoreboards do 
    resources :comments 
    resources :teams, only: [:edit, :create, :destroy, :update] 
end 

记分牌控制器方法在页面上哪一个可以发表评论:

def show 
    @scoreboard = Scoreboard.find_by_id(params[:id]) 
    @team = @scoreboard.teams.build 
    @comment = @scoreboard.comments.new 
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 

我将包括对祖先的宝石迁移,如果任何错误都上发了言:

class AddAncestryToComments < ActiveRecord::Migration 
    def change 
    add_column :comments, :ancestry, :string 
    add_index :comments, :ancestry 
    end 
end 

下面的代码显示视图代码:

记分牌#方式查看这是给我错误在最后一行:

<div class= "comment-section"> 
    <%= 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 %> #is it needed to include this here? because this form is for new comments not replies 
    <%= f.submit "Join the discussion...", class: " comment-button btn btn-primary" %>  
    <% end %> 

<%= nested_comments @scoreboard.comments.reject(&:new_record?).arrange(:order => :created_at) %> 
</div> 

(评论部分)_comment.html.erb查看:

<div class=" comment-div"> 
<p> Posted by <%= link_to "#{comment.user.name}", comment.user %> 
    <%= time_ago_in_words(comment.created_at) %> ago 
</p> 
<div class="comment-body"> 
    <%= comment.body %> 
    <%= link_to "Reply", new_scoreboard_comment_path(@scoreboard, comment, :parent_id => comment) %> 
</div> 
</div> 

助手方法来呈现注释:

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 

哪一个被重定向到的答复注释的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 %> 

在创建一个记分牌,我被重定向到显示页面,在那里我得到以下错误:

未定义的方法`排列'for []:Array

尽管注释数组为空,但如果它不是,我会得到相同的错误。我试过.subtree.arrange但这给了我同样的错误。此外,祖先文献说,.arrange只适用于有限范围的类。我不知道这意味着什么。我希望能够提供一些关于如何使页面工作的帮助,以便评论显示在他们的父母评论之后正确地排列了回复。如果这是线索评论(答复和所有)的错误方法,我希望能够接下来研究什么的一些指导。

回答

2

.reject(&:new_record?)这将返回一个数组。错误听起来像安排在ActiveRecord上的范围。所以移动reject到最后,它应该工作。

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

我一直在试图找出这个错误4天。你在5分钟内解决了它。非常感谢! – kpaul

+0

厚颜无耻的upvote从我 –

1

问候您的评论筑巢,我以前实现这一点,并找到了一个帮手的Railscasts建议是非常弱的。

Passing parent_id to a comment

相反,你最好使用部分取决于孩子的数量各自comment具有成为递归:

#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> 
+0

我其实是在想如何摆脱助手,在这里你有一个答案。我希望我能够接受两个答案。另外,如果您不介意,您是否可以解释以下行的工作方式:<%= link_to comment.title,comment_path(comment)%> – kpaul

+0

噢,对不起,该行只是一个可以使用的演示您的部分中的'comment'数据。你可以忽略它;否则你应该阅读如何在这里渲染集合:http://guides.rubyonrails.org/layouts_and_rendering.html#rendering-collections –

+1

很感谢!在另一篇文章中我也喜欢nested_dropdown的想法。真的很酷的东西。非常感谢你的帮助。 – kpaul

相关问题