运行Rail应用程序时出现以下错误。我相信它是由于Rails显示0而不是1的注释而引起的。记录0不存在。我想要做的就是编辑每条评论。由于零值导致的路由错误
我认为错误是我如何创建链接到嵌套的资源(即链接到帖子的评论)。
错误
“否路线匹配{:行动=>” 节目”,:控制器=> “评论”,:POST_ID => 1,:ID =>零} 尝试运行耙路线获取更多关于可用路线的信息。“
show.html.erb:
<% @post.comments.each do |c| %>
<p>
<b><%=h c.name %> said:</b><br />
<%= c.created_at %>
</p>
<p>
<%=h c.body %>
</p>
<p>
<%=h c.id %>
</p>
<%= link_to 'test', post_comment_path(:post_id => @post.id, :id => c.id) %> |
<%#= link_to 'Edit', edit_post_comment_path(:id => @comment.id,
:id => @post.id) %>
<%= link_to 'Comment', [@post, :comments ] %> |
<%= link_to 'Edit', edit_comment_path(@comment) %> |
<%= link_to 'Back', comments_path %>
当我删除此行,错误消失。
<%= link_to'test',post_comment_path(:post_id => @ post.id,:id => c.id)%> |
耙路输出:
C:\RUBY\RailsInstaller\Ruby1.9.3\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift)
C:\RUBY\RailsInstaller\Ruby1.9.3\bin\rake routes
posts_list GET /posts/list(.:format) posts#list
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
PUT /posts/:post_id/comments/:id(.:format) comments#update
DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
Process finished with exit code 0
UPDATE:
下现在的作品。我需要传递链接中的id值。
<%= link_to 'Edit', edit_post_comment_path(@post.id, c) %>
<%= link_to 'Destroy', [c.post, c], :confirm => 'Are you sure?', :method => :delete %>
请加你'$耙routes'输出。 –
“我相信这是因为Rails显示0而不是1而引起的评论” 这是错误的假设。 '@ post.comments'返回一个评论对象的集合,所以当你使用'each'遍历集合时,你会在每个循环中得到一个评论对象。它与数组中的项目位置无关。 –