Note that there are some explanatory texts on larger screens.

plurals
  1. POCRUD operation for nested model in rails 3
    primarykey
    data
    text
    <p>I am new in rails 3, and following the guide in ruby site I build the first blog application.</p> <p>However in the app,the model "Comment" do not have edit/update/delete operation.</p> <p>Then I tried to add it,but I fails.</p> <p>Instead of just generate model for model "Comment",I create the scaffold for model "Comment" using:</p> <pre><code>rails generate scaffold Comment commenter:string body:text post:references </code></pre> <p>And in the post.show page,I modify it like this:</p> <pre><code>&lt;% @post.comments.each do |comment| %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= comment.commenter %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= comment.body %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Edit', edit_comment_path(comment) %&gt;&lt;/td&gt; &lt;td&gt;&lt;%= link_to 'Destroy', comment, confirm: 'Are you sure?', method: :delete %&gt;&lt;/td&gt; &lt;/tr&gt; &lt;% end %&gt; </code></pre> <p>They are listed,but when I click the 'edit' or 'delete' link,it will try to jump to:</p> <pre><code>http://localhost:3000/comments/1 </code></pre> <p>And Then I will get the error:</p> <pre><code>No route matches [GET] "/comments/3/edit" or No route matches [DELETE] "/comments/3" </code></pre> <p>I have no idea now.</p> <p>Is there any demo out of box I can learn?</p> <hr> <p>UPdate:</p> <p>In the routes.rb:</p> <pre><code>resources :posts do resources :comments end </code></pre> <p>Note:the following is confided manually by myself. </p> <p>config generated by rails is :</p> <pre><code>resources :posts resources :comments </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload