簡體   English   中英

鏈接在localhost上有效,但在heroku上無效

[英]Link works on localhost but not heroku

很奇怪,直到今天,我在heroku上的應用程序都運行良好。 我一定有所改變。 我有3個模型:帖子,評論和問題。 指向問題索引的鏈接均無效(但在localhost上運行一切正常)。 heroku數據庫已遷移,並且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。 鏈接不適用於帖子頁面或評論頁面。 如果您需要我發布更多文件,請在評論中告訴我。

這行在您的questions#index #index中是錯誤的:

@question = Question.find params[:comment_id]

您正在嘗試根據評論ID查找問題,這可能不是您想要的。 問題是,您沒有任何其他ID可以使用。

如果運行rake routes ,則可以看到config/routes.rb文件生成的所有路由的列表。 您應該看到這樣的路線:

comment_questions GET comments/:comment_id/questions questions#index

這意味着當某人訪問諸如/comments/4/questions類的網址(可以使用輔助comment_questions_path生成)時,他們將調用QuestionsControllerindex方法,並且params[:comment_id]等於4。您的代碼試圖查找了ID 4的評論,並與ID 4的問題-但不是在你的數據庫ID 4個問題,所以應用程序崩潰。

@question應該指的是什么? 問題索引頁面通常應用於列出所有針對特定評論的問題,而不是其中的任何特定問題。


PS,您不需要包括內部do ... end在路線中do ... end

resources :comments do
  resources :questions do
  end
end

可以只是

resources :comments do
  resources :questions
end

您正在使用params[:comment_id]作為question_id params[:comment_id]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM