Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to make a blog which has users , posts and comments, I see some differences between what you did and what I did it before when I was creating a blog also. I will tell you what I did (by editing the code of the files you posted in your question) then try it if it works with you :)</p> <p>1- routes.rb make it like this</p> <pre><code>resources :users resources :posts do resources :comments end </code></pre> <p>2- user.rb is fine no need to be modified</p> <p>3- post.rb also is fine</p> <p>4- comments.rb</p> <pre><code>belongs_to :post belongs_to :user </code></pre> <p>5- posts_controller.rb</p> <pre><code> def show @post = Post.find(params[:id]) @comment = Comment.new end </code></pre> <p>6- view/posts/show.html.erb ( this view should make you able to see the post and the comments and a box for the new comments, and a link to edit post and a link to the posts index )</p> <pre><code>&lt;p&gt;&lt;strong&gt;Title:&lt;/strong&gt;&lt;%= @post.title %&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Text:&lt;/strong&gt;&lt;%= @post.text %&gt;&lt;/p&gt; &lt;h2&gt;Comments&lt;/h2&gt; &lt;%= render @posts.comments %&gt; &lt;h2&gt;Add a comment:&lt;/h2&gt; &lt;%= render "comments/form" %&gt; &lt;%= link_to 'Edit Post', edit_post_path(@post) %&gt; | &lt;%= link_to 'Back to Posts', posts_path %&gt; </code></pre> <p>7- comments_controller.rb ( don't forget to add the destroy method again )</p> <pre><code>class CommentsController &lt; ApplicationController before_filter :load_post def create @comment = @post.comments.build(params[:comment]) @comment.user_id = current_user.id if @comment.save redirect_to @post, notice: "Added comment." else render :new end end private def load_post @post = Post.find(params[:article_id]) end end </code></pre> <p>8- views/comments/_form.html.erb (just try to make it first in a simple way)</p> <pre><code>&lt;%= form_for([@post,@comment]) do |f| %&gt; &lt;p&gt; &lt;%= f.label :body %&gt;&lt;br /&gt; &lt;%= f.text_area :body %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.submit %&gt; &lt;/p&gt; &lt;% end %&gt; </code></pre> <p>9- views/comments/_comment.html.erb</p> <pre><code>&lt;p&gt;&lt;strong&gt;Commenter:&lt;/strong&gt;&lt;%= comment.user.email %&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Comment:&lt;/strong&gt;&lt;%= comment.body %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= link_to 'Destroy Comment', [comment.post, comment],method: :delete, data: { confirm: 'Are you sure?' } %&gt; &lt;/p&gt; </code></pre> <p>I hope this work with you as it works with me, try it and let me know how it goes with you, i got my blog work from <a href="https://github.com/railscasts/229-polling-for-changes-revised/tree/master/blog-before" rel="nofollow">the before code for revised episode 229</a>.</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.
    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