title: Ruby on Rails - Adding comments to post
body: <p>Trying to learn RoR. Currently adding comments to posts by user. So far I have a posts model, comments model and post_comments model (to associate the two). So for in 'rails console' I can run: (say I set p = Post.first and c = Comment.first)</p>
  
  <blockquote>
    <p>p.comments &lt;&lt; c</p>
  </blockquote>
  
  <p>This forms an association so it works in the console. I just can't seem to get the comments to form this association from the UI. So far I am creating the comments at "comments/new" (not sure if this is the issue. Do they need to be created on the "show view" for "post").</p>
  
  <p>Here are some code snippets</p>
  
  <blockquote>
    <p>Controllers
    comments_controller.rb</p>
  </blockquote>
  
  <pre><code>class CommentsController &lt; ApplicationController
  
  def index
    @comment = Comment.all
  end
  
  def new
    @comment = Comment.new
  end
  
  def create
    @comment = Comment.new(commentParams)
    if @comment.save
      flash[:success] = "Comment successfully added"
      redirect_to comments_path(@comment)
    else
      render 'new'
    end
  end
  
  def show
    @comment = Comment.find(params[:id])
  end
  
  private
  
    def commentParams
      params.require(:comment).permit(:comment)
    end
  end
  </code></pre>
  
  <blockquote>
    <p>posts_controller</p>
  </blockquote>
  
  <pre><code>class PostsController &lt; ApplicationController
  before_action :setPost, only: [:edit, :update, :show, :destroy, :sold]
  before_action :requireUser, except: [:index, :show]
  before_action :requireSameUser, only: [:edit, :update, :destroy, :sold]
  
  def index
    @posts = Post.paginate(page: params[:page], per_page: 20)
  end
  
  def new
    @post = Post.new
  end
  
  def create
    @post = Post.new(postParams)
    @post.user = currentUser
    if @post.save
      flash[:success] = "Post successfully added."
      redirect_to post_path(@post)
    else
      render 'new'
    end
  end
  
  def update
    if @post.update(postParams)
      flash[:success] = "Post successfully updated."
      redirect_to post_path(@post)
    else
      render 'edit'
    end
  end
  
  def show
  end
  
  def edit
  end
  
  def sold
    @post.toggle(:sold)
    @post.save
    redirect_to post_path(@post)
  end
  
  def destroy
    @post.destroy
    flash[:danger] = "Item successfully deleted."
    redirect_to posts_path
  end
  
  private
    def postParams
      params.require(:post).permit(:title, :price, :description,   category_ids:[])
    end
  
    def setPost
      @post = Post.find(params[:id])
    end
  
    def requireSameUser
      if currentUser != @post.user and !currentUser.admin
        flash[:danger] = "You can only edit or delete your own items"
        redirect_to root_path
      end
    end
  end
  </code></pre>
  
  <blockquote>
    <p>Models
    comment.rb</p>
  </blockquote>
  
  <pre><code>class Comment &lt; ActiveRecord::Base
    belongs_to :post_comments
    belongs_to :user
    belongs_to :post
  end
  </code></pre>
  
  <blockquote>
    <p>post_comment.rb</p>
  </blockquote>
  
  <pre><code>class PostComment &lt; ActiveRecord::Base
    belongs_to :post
    belongs_to :comment
  end
  </code></pre>
  
  <blockquote>
    <p>post.rb</p>
  </blockquote>
  
  <pre><code>class Post &lt; ActiveRecord::Base
    belongs_to :user
    has_many :post_categories
    has_many :categories, through: :post_categories
    has_many :post_comments
    has_many :comments, through: :post_comments  
  
    validates :title, presence: true,
                  length: { minimum: 4, maximum: 20 }
  
    validates :description, presence: true,
                        length: { maximum: 1000 }
    validates :user_id, presence: true
  </code></pre>
  
  <p>end</p>
  
  <blockquote>
    <p>Views
    posts/show.html.erb</p>
  </blockquote>
  
  <pre><code>  &lt;p&gt;Comments: &lt;%= render @post.comments %&gt;&lt;/p&gt;
  </code></pre>
  
  <p>This renders the partial below</p>
  
  <blockquote>
    <p>comments/_comment.html.erb
    &lt;%= link_to comment.name, comment_path(comment) %> &nbsp;</p>
  </blockquote>
  
  <p>Finally is the new comment page as it is.</p>
  
  <blockquote>
    <p>comments/new.html.erb</p>
  </blockquote>
  
  <pre><code>&lt;h1&gt;New Comment&lt;/h1&gt;
  
  &lt;%= render 'shared/errors', obj: @comment %&gt;
  
  &lt;div class="row"&gt;
  &lt;div class="col-xs-12"&gt;
    &lt;%= form_for(@comment, :html =&gt; {class: "form-horizontal", role: "form"}) do |f| %&gt;
  
    &lt;div class="form-group"&gt;
      &lt;div class="control-label col-sm-2"&gt;
        &lt;%= f.label :comment %&gt;
      &lt;/div&gt;
  
      &lt;div class="col-sm-8"&gt;
        &lt;%= f.text_area :comment, rows: 3, class: "form-control", placeholder: "Please enter a comment", autofocus: true %&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div class="form-group"&gt;
      &lt;div class="center col-sm-offset-1 col-sm-10"&gt;
        &lt;%= f.submit class: "btn btn-primary btn-lg" %&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>
  </p>
  
  <p>Any help would be greatly received.</p>
  
  <p>Update </p>
  
  <blockquote>
    <p>Log</p>
  </blockquote>
  
  <pre><code>Started GET "/posts/2" for ::1 at 2016-01-15 12:39:55 +0000
  Processing by PostsController#show as HTML
  Parameters: {"id"=&gt;"2"}
  [1m[36mPost Load (0.1ms)[0m  [1mSELECT  "posts".* FROM "posts" WHERE    "posts"."id" = ? LIMIT 1[0m  [["id", 2]]
  [1m[35mUser Load (0.1ms)[0m  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  [1m[36m (0.1ms)[0m  [1mSELECT COUNT(*) FROM "posts" WHERE "posts"."user_id" = ?[0m  [["user_id", 1]]
  [1m[35mCategory Exists (0.1ms)[0m  SELECT  1 AS one FROM "categories"    INNER JOIN "post_categories" ON "categories"."id" =  "post_categories"."category_id" WHERE "post_categories"."post_id" = ?  LIMIT 1  [["post_id", 2]]
  [1m[36mCategory Load (0.0ms)[0m  [1mSELECT "categories".* FROM "categories" INNER JOIN "post_categories" ON "categories"."id" = "post_categories"."category_id" WHERE "post_categories"."post_id" = ?[0m  [["post_id", 2]]
  Rendered categories/_category.html.erb (0.2ms)
  [1m[35mComment Load (0.1ms)[0m  SELECT "comments".* FROM "comments"  WHERE "comments"."post_id" = ?  [["post_id", 2]]
  Rendered comments/_comment.html.erb (0.1ms)
  Rendered posts/show.html.erb within layouts/application (6.5ms)
  [1m[36mCategory Load (0.1ms)[0m  [1mSELECT "categories".* FROM "categories"[0m
  Rendered layouts/_navigation.html.erb (0.9ms)
  Rendered layouts/_messages.html.erb (0.1ms)
  Rendered layouts/_footer.html.erb (0.1ms)
  Completed 200 OK in 52ms (Views: 50.3ms | ActiveRecord: 0.5ms)
  </code></pre>