Ruby on rails 链接到问题索引错误
我有3种型号:Ruby on rails 链接到问题索引错误,ruby-on-rails,Ruby On Rails,我有3种型号: 职位 评论 问题 评论属于帖子,问题属于评论。在我的帖子索引页面上,我显示了所有帖子以及每个帖子的最后一条评论。我的问题出现在尝试将最后一条评论链接到问题索引页面时。 这就是我试图做到的: <%= link_to (post.comments.last.try(:[],:body)), comment_questions_path(@comment) %> 这是我的routes.rb文件: resources :posts do resources :
- 职位
- 评论
- 问题
<%= link_to (post.comments.last.try(:[],:body)), comment_questions_path(@comment) %>
这是我的routes.rb
文件:
resources :posts do
resources :comments
end
resources :comments do
resources :questions
end
当我运行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
使用这样的路线时要小心!您的CommentsController必须使用
:post\u id
(嵌套)和:post\u id
响应请求。它应该看起来更像:
resources :posts do
resources :comments do
resources :questions
end
end
我不知道如何在
/posts
页面的posts#index
操作中加载@comment
变量。您正在循环浏览每个帖子
,除非您正在执行以下操作:
@comment=post.comments.last
对于页面中的每个post
,您的链接将无法工作
您的链接需要如下所示:
<%= link_to (post.comments.last.try(:[],:body)), comment_questions_path(post.comments.last) %>
需要你的应用程序路由文件,也可以在控制台中运行rake路由,并将结果发布在你的问题中或其他地方,如gist或pastie.orgOk,两者都发布。请告诉我您是否需要rake routes的更多内容。因此,单击该链接后,您期望的url应该是:localhost:3000/comments/5/questions,对吗?另外,请张贴发送请求的服务器日志,需要查看发送到服务器的参数。以及单击后重定向到的url是什么?是localhost:3000/comments/questions吗?我重定向到的url是localhost:3000/comments//questions,但是的,我希望它是localhost:3000/comments/5/questions。服务器日志已张贴在上面。是否有任何方法可以让我的服务器在不更改路由的情况下正常工作,或者将它们全部更改为我的最佳选择?抱歉,响应太晚。改变它们是你最好的选择。如果你从一开始就正确地设置了路线,那么以后你会很感激的。这让事情变得容易多了
<%= link_to (post.comments.last.try(:[],:body)), comment_questions_path(post.comments.last) %>