我在我的应用程序中使用嵌套资源,如下所示:
resources :comments do
resources :questions
end
resources :questions do
resources :answers
end
在我的评论节目页面上,我试图获得属于评论的答案总数(不是问题)。但
<%= @comment.answers.count %>
给了我这个错误:
ActionView::Template::Error (undefined method `answers' for #<Comment:0x00000102669948>)
我不确定如何在不改变路线的情况下获得comment.answers。这是我的评论控制器中的show函数:
def show
@comment = Comment.find params[:id]
@questions = @comment.questions.order
end
这是我得到的错误堆栈跟踪:
SELECT COUNT(*) FROM "votes" WHERE "votes"."voter_id" = ? AND "votes"."voter_type" = ? AND "votes"."votable_id" = 115 AND "votes"."votable_type" = 'Question' AND "votes"."vote_scope" IS NULL [["voter_id", 17], ["voter_type", "User"]]