1

奇怪的是,直到今天,我在 heroku 上的应用程序都运行良好。我一定改变了什么。我有 3 个模型:帖子、评论和问题。指向问题索引的链接都不起作用(但在本地主机上运行一切正常)。heroku db 已迁移,并且 url 将转到正确的位置。这是我得到的heroku日志错误:

ActiveRecord::RecordNotFound (Couldn't find Question with id=4)

页面上说:

The page you were looking for doesn't exist.

以下是链接的样子:

<%= link_to (comment.body), comment_questions_path(comment) %>

这是问题#index:

def index
  @comment = Comment.find params[:comment_id]
  @questions = @comment.questions
  @question = Question.find params[:comment_id]
end

以下是路线:

 resources :posts do
  resources :comments do
  end
 end

 resources :comments do
  resources :questions do
 end
 end

我认为问题在于它正在寻找问题 ID,即使它是问题索引页面。链接在帖子页面或评论页面中不起作用。如果您需要我发布更多文件,请在评论中告诉我。

4

2 回答 2

2

这条线在你的questions#index

@question = Question.find params[:comment_id]

您正在尝试根据评论 ID 查找问题,这可能不是您想要的。问题是,您也没有其他可以使用的 ID。

如果您运行rake routes,您可以看到config/routes.rb文件生成的所有路由的列表。你应该看到这样的路线:

comment_questions GET comments/:comment_id/questions questions#index

这意味着当有人访问类似的 url /comments/4/questions(可以使用 helper 生成comment_questions_path)时,他们将调用 的index方法QuestionsController,并且params[:comment_id]等于 4。您的代码正在尝试查找 ID 为 4 的评论和 ID 为 4问题 - 但是您的数据库中没有 ID 为 4 的问题,因此应用程序崩溃。

@question无论如何应该指的是什么?问题索引页面通常应该用于列出特定评论的所有问题,而不是其中任何特定的问题。


PS您不需要do ... end在路线中包含内部:

resources :comments do
  resources :questions do
  end
end

只能是

resources :comments do
  resources :questions
end
于 2013-10-29T03:29:21.557 回答
0

您正在使用params[:comment_id]asquestion_id

于 2013-10-29T01:48:17.483 回答