1
votes

I create user->posts->comments associations, but comments don't work right.When I commented post, comment save to data base, column user_id was recorded (ok), but column post_id is empty.

There is partial _post_list.html.erb

<% @posts.each do |post| %>
    <tr>
      <td><%= post.title %></td>
      <td><%= post.content %></td>
      <%= render 'shared/comment' %>
      <%= post.user.name %>
      </tr></br></br>
      <% end %>  

There is partial _comment.html.erb

<%= form_for(@comment) do |f| %>
<%= f.text_area :body %><br><br>
<%= f.submit "commented" %>
<% end %>

controller comments_controller

class CommentsController < ApplicationController
    def new
        @comment = Comment.new
    end

    def create
        @comment = current_user.comments.build(comment_params)
        if @comment.save
            redirect_to root_url
        else
            render 'static_pages/home'
        end
    end

     private

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

UPD there is server log

Started POST "/posts/6/comments" for 127.0.0.1 at 2014-11-16 23:28:26 +0300
Processing by CommentsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZvyHKxSdH0a1rf79yaWmo9N/pD9/YhSQ9Ek8bdMLhOI=", "comment"=>{"body"=>"stackoverflow"}, "commit"=>"commented", "post_id"=>"6"}
  User Load (4.9ms)  SELECT  "users".* FROM "users"  WHERE "users"."remember_token" = 'b52623cb00671708536fce96c310b5d365128880' LIMIT 1
   (0.2ms)  begin transaction
  SQL (0.6ms)  INSERT INTO "comments" ("body", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?)  [["body", "stackoverflow"], ["created_at", "2014-11-16 20:28:26.544500"], ["updated_at", "2014-11-16 20:28:26.544500"], ["user_id", 5]]
   (131.7ms)  commit transaction
Redirected to http://localhost:3000/
Completed 302 Found in 152ms (ActiveRecord: 137.4ms)


Started GET "/" for 127.0.0.1 at 2014-11-16 23:28:26 +0300
Processing by StaticPagesController#home as HTML
  User Load (0.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."remember_token" = 'b52623cb00671708536fce96c310b5d365128880' LIMIT 1
  Rendered shared/_post_form.html.erb (2.4ms)
  Post Load (0.4ms)  SELECT "posts".* FROM "posts"   ORDER BY created_at DESC
  Rendered shared/_comment.html.erb (1.8ms)
  User Load (0.3ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 5]]
  Rendered shared/_comment.html.erb (2.8ms)
  User Load (0.2ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Rendered shared/_comment.html.erb (4.0ms)
  CACHE (3.1ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Rendered shared/_comment.html.erb (1.5ms)
  CACHE (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Rendered shared/_comment.html.erb (2.9ms)
  CACHE (0.0ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Rendered shared/_post_list.html.erb (45.3ms)
  Rendered static_pages/home.html.erb within layouts/application (52.1ms)
  Rendered layouts/_header.html.erb (1.2ms)
Completed 200 OK in 364ms (Views: 347.0ms | ActiveRecord: 4.5ms)

controller static_pages_controller class StaticPagesController < ApplicationController def index end

    def show
    end

    def home
    @post = current_user.posts.build 
    if signed_in?
    end
    @posts = Post.all
    @comment = current_user.comments.build
    end

end

view home.html.erb

<%= render 'shared/post_form' %>
<%= render 'shared/post_list' %>

How fix? sorry for my bad English

2
Can you post your controller code? You aren't telling @comment which @post it belongs to. Once you post your controller, we can tell you what needs to be changed. - Alec Sanger
comments_controller or posts_controller? - vveare138
comments, assuming that's where your create action is - Alec Sanger

2 Answers

1
votes

You are far from showing enough code to allow us to understand what can happen. But I feel strange that your form does not include somewhere a reference to post.id or whatever you name the identifier for post objects.

The controller will only be able to use :

  • input fields of the form (here text of comment)
  • session stored values (user id)

If the current post id is neither in form fields, not in session, the controller will not get id, it you cannot store it in database.

0
votes

It looks like you are looping through all of your posts and creating a comment form on the same page for each. You may want to set your comment routes up as nested resources by wrapping it within the post resource.

# config/routes.rb
resources :posts do
  resources :comments
end

Pass your @post object into the partial like so:

<%= render 'shared/comment', post: post %>

Then you should be able to create your comment form like so:

<%= form_for([post,@comment]) do |f| %>
<%= f.text_area :body %><br><br>
<%= f.submit "commented" %>
<% end %>

By including that post, you are telling the comment which post it belongs to. Don't forget to update your strong parameters in the controller if you are using them.

EDIT: Based on your update, you'll need to add :post_id to the permit at the bottom of our controller.