我一直在尝试解决与在我的应用程序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只适用于有限范围的类。我不知道这意味着什么。我希望能够提供一些关于如何使页面工作的帮助,以便评论显示在他们的父母评论之后正确地排列了回复。如果这是线索评论(答复和所有)的错误方法,我希望能够接下来研究什么的一些指导。
我一直在试图找出这个错误4天。你在5分钟内解决了它。非常感谢! – kpaul
厚颜无耻的upvote从我 –