2013-09-25 32 views
1

我有3種型號:LINK_TO問題索引錯誤

  • 帖子
  • 評論
  • 問題

評述屬於職位和問題屬於意見。在我的帖子索引頁面上,我顯示了所有帖子以及屬於每個帖子的最後評論。嘗試將最後一條評論鏈接到問題索引頁時,我遇到了問題。 這是我正在努力做到這一點:

<%= link_to (post.comments.last.try(:[],:body)), comment_questions_path(@comment) %> 

錯誤我收到:

Couldn't find Comment with id=questions 

這裏是我的routes.rb文件:

resources :posts do 
    resources :comments 
end 

resources :comments do 
    resources :questions 
end 

comment_questions_path時我運行rake routes

comment_questions GET /comments/:comment_id/questions(.:format) questions#index 

服務器日誌:

 Started GET "/comments//questions" for 127.0.0.1 at 2013-09-25 20:23:14 -0400 
    ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"  
    Processing by CommentsController#show as HTML 
    Parameters: {"id"=>"questions"} 
    Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = ? LIMIT 1  [["id", "questions"]] 
    Completed 404 Not Found in 66ms 
+0

需要你的應用程序文件的航線,也跑耙航線控制檯和發佈結果在你的問題或其他一些地方一樣要點或pastie.org – rmagnum2002

+0

好,貼都。告訴我你是否需要更多的耙路線。 – user2759575

+0

,所以你點擊鏈接後的url應該是這樣的:localhost:3000/comments/5/questions,對吧?還請發送請求正在發送的服務器日誌,需要查看哪些參數正在發送到服務器。 – rmagnum2002

回答

0

我看不出你如何能在/posts頁,posts#index動作加載@comment變量。你正在循環每個post,所以除非你這樣做:

@comment = post.comments.last爲每個post在您的網頁,您的鏈接將無法正常工作。

你的鏈接需要看起來像:

<%= link_to (post.comments.last.try(:[],:body)), comment_questions_path(post.comments.last) %> 
0

使用這樣的路由時一定要小心!您的評論控制器將不得不迴應請求:post_id(嵌套)沒有:post_id。它真的應該更像:

resources :posts do 
    resources :comments do 
    resources :questions 
    end 
end 
+0

有沒有什麼方法可以讓我的工作不需要改變路線,或者正在改變他們所有我最好的賭注? – user2759575

+0

對不起,遲到的迴應。改變它們是你最好的選擇。如果您從一開始就正確地設置了路線,您將會在路途中感激不盡。它使事情變得更容易 –