I am getting the below error when running a Rail App. I believe it is being caused because Rails is showing comments from 0, instead of 1. The record 0 does not exist. All I want to do is make it possible to edit each comment.
I think the error is how I am creating the links to nested resources (ie linking to comments from posts).
Error
“No route matches {:action=>”show”, :controller=>”comments”, :post_id=>1, :id=>nil}
Try running rake routes for more information on available routes.”
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 %>
When I remove this line, the error goes away.
<%= link_to ‘test’, post_comment_path(:post_id => @post.id, :id => c.id) %> |
rakes routes output:
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:
The following now works. I needed to pass the id values within the link.
<%= link_to 'Edit', edit_post_comment_path(@post.id, c) %>
<%= link_to 'Destroy', [c.post, c], :confirm => 'Are you sure?', :method => :delete %>
Try this code for link generation
or even