Erik V
Erik V

Reputation: 385

Rails comments do not save

Edit: Log from form submission:

Started POST "/posts/1/comments" for 141.161.133.10 at 2015-03-19 17:10:19 +0000
Processing by CommentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"9laIJxseq/x+7C3iJAdpvalFvMgN397lWC7c/6Ha/hEjKTE9A3e19liFlwGzZjePReFdh2F1fLj1itZzRjixgQ==", "comment"=>{"body"=>"Hi stackoverflow"}, "commit"=>"Submit", "post_id"=>"1"}
   (0.3ms)  begin transaction
  SQL (1.9ms)  INSERT INTO "comments" ("body", "created_at", "updated_at") VALUES (?, ?, ?)  [["body", "Hi stackoverflow"], ["created_at", "2015-03-19 17:10:19.808006"], ["updated_at", "2015-03-19 17:10:19.808006"]]
   (32.1ms)  commit transaction
Redirected to https://blog-erikvdw.c9.io/posts.10
Completed 302 Found in 48ms (ActiveRecord: 34.2ms)

And comment_params method:

def comment_params
  params.require(:comment).permit(:body)
end

I've looked around quite a while in a github repo and other SO questions similar to what I am doing, but haven't seen exactly what I'm doing wrong. My problem is that comments are not being created when the comments form is submitted in Posts/Show. I presume it to be an issue with an activerecord association:

Posts/Show

<% content_for(:title, @post.title ) %>
<p id="notice"><%= notice %></p>

<h1>Blog</h1>
<br>
<div>
  <%= image_tag @post.image.full.url %>
</div>
<div class="panel panel-default">
  <div class="panel-heading">
    <h2><em><%= @post.title %></em></h2>
  </div>
  <div class="panel-body">
    <p><%= @post.content %></p>
  </div>
</div>

<%= link_to 'Edit', edit_post_path(@post) %> |
<%= link_to 'Back', posts_path %>

<% @post.comments.each do |comment| %>
  <%= comment.body %>
<% end %>

<%= form_for [@post, @post.comments.build] do |f| %>
  <%= f.text_area :body %>
  <%= f.submit "Submit" %>
<% end %>
<p>Comments: <%= @post.comments.count %></p>

Posts Model

class Post < ActiveRecord::Base
    has_many :comments, dependent: :destroy
    mount_uploader :image, PostUploader 

end

Comments Model

class Comment < ActiveRecord::Base
    belongs_to :user
    belongs_to :post
end

Schema File

create_table "comments", force: :cascade do |t|
    t.string   "body"
    t.integer  "user_id"
    t.integer  "employee_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
    t.integer  "post_id"
  end

  add_index "comments", ["employee_id"], name: "index_comments_on_employee_id"
  add_index "comments", ["post_id"], name: "index_comments_on_post_id"
  add_index "comments", ["user_id"], name: "index_comments_on_user_id"

  create_table "posts", force: :cascade do |t|
    t.string   "title"
    t.text     "content"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string   "image"
  end

  create_table "users", force: :cascade do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.string   "username"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

Comments_Controller New and Create Actions

def new
    @comment = Comment.new
  end

  # POST /comments
  # POST /comments.json
  def create
    @comment = Comment.new(comment_params)

    respond_to do |format|
      if @comment.save
        format.html { redirect_to posts_path(@comment), notice: 'Comment was successfully created.' }
        format.json { render :show, status: :created, location: @comment }
      else
        format.html { render :new }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

Upvotes: 1

Views: 463

Answers (1)

RichardAE
RichardAE

Reputation: 2970

You're not creating the Comment through the specific Post, so no association is being made. Try this in your create action:

@comment = Post.find(params[:post_id]).comments.new(comment_params)

This will populate the post_id in your comments table automatically which will then link it to the Post.

Upvotes: 1

Related Questions