我有3个型号:
评论属于帖子,问题属于评论。在我的帖子索引页面上,我显示了所有帖子以及属于每个帖子的最后评论。尝试将最后一条评论链接到问题索引页面时出现问题。 这就是我试图这样做的方式:
<%= 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 :(得分:0)
使用这样的路线时要小心!您的CommentsController必须使用:post_id
(嵌套)和而不是:post_id
来回复请求。它应该看起来更像:
resources :posts do
resources :comments do
resources :questions
end
end
答案 1 :(得分:0)
我看不到如何在@comment
页面/posts
操作中加载posts#index
变量。你循环遍历每个post
,除非你这样做:
@comment = post.comments.last
对于您网页中的每个post
,您的链接都无效。
您的链接需要看起来像:
<%= link_to (post.comments.last.try(:[],:body)), comment_questions_path(post.comments.last) %>