0

运行 Rail App 时出现以下错误。我相信这是因为 Rails 显示的是 0 而不是 1 的评论。记录 0 不存在。我想做的就是让编辑每条评论成为可能。

我认为错误在于我如何创建指向嵌套资源的链接(即链接到帖子中的评论)。

错误

"没有路由匹配 {:action=>"show", :controller=>"comments", :post_id=>1, :id=>nil} 尝试运行 rake 路由以获取有关可用路由的更多信息。"

显示.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

更新:

以下现在有效。我需要在链接中传递 id 值。

<%= link_to 'Edit', edit_post_comment_path(@post.id, c) %>

<%= link_to 'Destroy', [c.post, c], :confirm => 'Are you sure?', :method => :delete %>
4

1 回答 1

0

试试这个代码生成链接

<%= link_to 'test', post_comment_path(@post, c) %>

甚至

<%= link_to 'test', [@post, c] %>
于 2012-09-18T20:44:31.777 回答