I am new in rails 3, and following the guide in ruby site I build the first blog application.
However in the app,the model “Comment” do not have edit/update/delete operation.
Then I tried to add it,but I fails.
Instead of just generate model for model “Comment”,I create the scaffold for model “Comment” using:
rails generate scaffold Comment commenter:string body:text post:references
And in the post.show page,I modify it like this:
<% @post.comments.each do |comment| %>
<tr>
<td><%= comment.commenter %></td>
<td><%= comment.body %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
They are listed,but when I click the ‘edit’ or ‘delete’ link,it will try to jump to:
http://localhost:3000/comments/1
And Then I will get the error:
No route matches [GET] "/comments/3/edit" or
No route matches [DELETE] "/comments/3"
I have no idea now.
Is there any demo out of box I can learn?
UPdate:
In the routes.rb:
resources :posts do
resources :comments
end
Note:the following is confided manually by myself.
config generated by rails is :
resources :posts
resources :comments
Why I modify it is that in the comment build form,the post url should be “/posts/1/comments” for create new Comment,otherwise the post url will be “/comments” which will not associate the post and the comment.
Did you configure your
routes? Yourconfig/routes.rbshould containyou can also run
rake routesto see what are the available url for your application based on your resource configuration.Edit:
For a demo you can try this video on youtube. However, you can find lots of videos in web regarding this.
Edit:
So seems you need your comment resource in two ways. Both as a nested resource of posts and top level resource as well. So you can have two things together then