Home>

I have implemented the comment function of the bulletin board version, but the comment is reflected in all posts.

As I researched various things and rewrote the code, I couldn't collect the code, and I came up with a question.
I want you to help me. Thank you.


Log

Started POST "/posts /22 /comments" for 118.103.63.140 at 2021-09-15 22:33:35 +0000
I, [2021-09-15T22: 33: 35.071026 # 8361] INFO-: Cannot render console from 118.103.63.140! Allowed networks: 127.0.0.1, :: 1, 127.0.0.0 /127.255.255.255
I, [2021-09-15T22: 33: 35.072179 # 8361] INFO-: Processing by CommentsController # create as HTML
I, [2021-09-15T22: 33: 35.072350 # 8361] INFO-: Parameters: {"utf8"= >
"✓", "authenticity_token"= >
"StrqMsplWA1TrM02RpbYnS + ANZLIMQJIbcu /S9mHG5AlI1PrZqF740APr1xYAGQKQcLC /b + DCBhL9SIahtBVIw==", "comment"= >
{"post_id"= >
"22", "content"= >
"Test k eyes"},
 "commit"= >
"Comment", "post_id"= >
"twenty two"}
D, [2021-09-15T22: 33: 35.073722 # 8361] DEBUG-: User Load (0.3ms) SELECT `users`. * FROM` users` WHERE `users`.` id`= 62 LIMIT 1
D, [2021-09-15T22: 33: 35.074274 # 8361] DEBUG-: ↳ app /helpers /sessions_helper.rb: 3
D, [2021-09-15T22: 33: 35.074827 # 8361] DEBUG-: Unpermitted parameter:: post_id
D, [2021-09-15T22: 33: 35.075816 # 8361] DEBUG-: # <
ActiveModel :: Errors: 0x00007f68004d5cc8 @base= # <
Comment id: nil, content: "test k eyes", user_id: 62, post_id: nil, created_at: nil, updated_at: nil >
, @messages= {},
 @details= {} >
D, [2021-09-15T22: 33: 35.076884 # 8361] DEBUG-: (0.2ms) BEGIN
D, [2021-09-15T22: 33: 35.077429 # 8361] DEBUG-: ↳ app /controllers /comments_controller.rb: 11
D, [2021-09-15T22: 33: 35.079450 # 8361] DEBUG-: Comment Create (0.8ms) INSERT INTO `comments` (`content`, `user_id`,` created_at`, `updated_at`) VALUES (' Test k eyes', 62, '2021-09-15 22:33:35', '2021-09-15 22:33:35')')
D, [2021-09-15T22: 33: 35.080408 # 8361] DEBUG-: ↳ app /controllers /comments_controller.rb: 11
D, [2021-09-15T22: 33: 35.084456 # 8361] DEBUG-: (2.6ms) COMMIT
D, [2021-09-15T22: 33: 35.085211 # 8361] DEBUG-: ↳ app /controllers /comments_controller.rb: 11
I, [2021-09-15T22: 33: 35.086136 # 8361] INFO-: Redirected to https://ac7c34ad0f0d4d6c88e67a1fd746ee4a.vfs.cloud9.ap-northeast-1.amazonaws.com/posts/22
I, [2021-09-15T22: 33: 35.086931 # 8361] INFO-: Completed 302 Found in 14ms (ActiveRecord: 3.9ms)

Comment posting screen (show.html.erb)

<
h1 >
Post details page <
/h1 >
≪
h3 >
≪
%= @ post.shopcontent% >
≪
/h3 >
≪
h2 >
Comment list <
/h2 >
≪
% @ comments.each do | c |% >
  ≪
div >
    ≪
%= c.content% >
    ≪
hr >
  ≪
/div >
≪
% end% >
≪
%= form_with model: [@post, @ comment] do | f |% >
    ≪
%= f.label: content,'comment content'% >
    ≪
%= f.hidden_field: post_id, value: @ post.id% >
    ≪
%= f.text_area: content% >
    ≪
br >
    ≪
%= f.submit'comment'% >
≪
% end% >
≪
%= link_to "Return to Home", posts_path% >

comment controller

class CommentsController <
 ApplicationController
   before_action: require_user_logged_in
  def new
     @comment= Comment.new
  end
  def create
     @comment= Comment.new (comment_params)
     @ comment.user_id= current_user.id
     logger.debug @ comment.errors.inspect
    if @ comment.save
     redirect_back (fallback_location: root_path)
    else else
      @comments= Comment.all
      redirect_back (fallback_location: root_path)
    end
  end
private
  def comment_params
    params.require (: comment) .permit (: content)
  end
end

postscontroller (show action excerpt)

def show
    @post= Post.find (params [: id])
    @comments= @ post.comments
    @comment= Comment.new
    @comments= Comment.all
  end

Each model (User, Post, Comment)


class User <
 ApplicationRecord
    has_secure_password
    has_many: posts
    has_many: comments
end
class Post <
 ApplicationRecord
    belongs_to: user, optional: true
    validates: shopname, presence: true, length: {maximum: 20}
    validates: shopaddress, presence: true, length: {maximum: 30}
    validates: shopcontent, presence: true, length: {maximum: 300}
    has_many: comments
end
class Comment <
 ApplicationRecord
  validates: content, presence: true, length: {maximum: 200}
  belongs_to: user, optional: true
  belongs_to: post, optional: true
end

Routing

Rails.application.routes.draw do
  get'toppages /index'
  root to:'toppages # index'
  get'login', to:'sessions # new'
  post'login', to:'sessions # create'
  delete'logout', to:'sessions # destroy'
  get'signup', to:'users # new'
  resources: users, only: [: index,: show,: create]
  resources: posts do
   resources: comments, only: [: create]
  end
  get "search", to: "posts # search"
end

@comments= Log if you want to delete Comment.all

I, [2021-09-16T06: 55: 29.931617 # 5355] INFO-: Started POST "/posts /21 /comments" for 61.215.151.83 at 2021-09-16 06:55:29 +0000
I, [2021-09-16T06: 55: 29.933608 # 5355] INFO-: Cannot render console from 61.215.151.83! Allowed networks: 127.0.0.1, :: 1, 127.0.0.0 /127.255.255.255
I, [2021-09-16T06: 55: 29.936336 # 5355] INFO-: Processing by CommentsController # create as HTML
I, [2021-09-16T06: 55: 29.937392 # 5355] INFO-: Parameters: {"utf8"= >
"✓", "authenticity_token"= >
"QHHpPXn7O4eNqBuwNpjScx7GY0nXd9P0k7adKReh8BeAdCOZmQODT1uGr /N3sOxG1AgQM /oMAMX4tI34umclwg=", "comment"= >
{"post_id"= >
"21", "content"= >
"Test comment"},
 "commit"= >
"Comment", "post_id"= >
"twenty one"}
D, [2021-09-16T06: 55: 29.940419 # 5355] DEBUG-: User Load (0.3ms) SELECT `users`. * FROM` users` WHERE `users`.` id`= 62 LIMIT 1
D, [2021-09-16T06: 55: 29.941195 # 5355] DEBUG-: ↳ app /helpers /sessions_helper.rb: 3
D, [2021-09-16T06: 55: 29.942189 # 5355] DEBUG-: Unpermitted parameter:: post_id
D, [2021-09-16T06: 55: 29.943125 # 5355] DEBUG-: # <
ActiveModel :: Errors: 0x000056208e419010 @base= # <
Comment id: nil, content: "test comment", user_id: 62, post_id: nil, created_at: nil, updated_at: nil >
, @messages= {},
 @details= {} >
D, [2021-09-16T06: 55: 29.944758 # 5355] DEBUG-: (0.8ms) BEGIN
D, [2021-09-16T06: 55: 29.945495 # 5355] DEBUG-: ↳ app /controllers /comments_controller.rb: 11
D, [2021-09-16T06: 55: 29.947243 # 5355] DEBUG-: Comment Create (0.2ms) INSERT INTO `comments` (`content`, `user_id`,` created_at`, `updated_at`) VALUES (' Test Comments', 62, '2021-09-16 06:55:29', '2021-09-16 06:55:29')
D, [2021-09-16T06: 55: 29.949058 # 5355] DEBUG-: ↳ app /controllers /comments_controller.rb: 11
D, [2021-09-16T06: 55: 29.953294 # 5355] DEBUG-: (3.3ms) COMMIT
D, [2021-09-16T06: 55: 29.954079 # 5355] DEBUG-: ↳ app /controllers /comments_controller.rb: 11
I, [2021-09-16T06: 55: 29.954959 # 5355] INFO-: Redirected to https://ac7c34ad0f0d4d6c88e67a1fd746ee4a.vfs.cloud9.ap-northeast-1.amazonaws.com/posts/21
I, [2021-09-16T06: 55: 29.955739 # 5355] INFO-: Completed 302 Found in 17ms (ActiveRecord: 4.7ms)
I, [2021-09-16T06: 55: 30.069278 # 5355] INFO-: Started GET "/posts /21" for 61.215.151.83 at 2021-09-16 06:55:30 +0000
I, [2021-09-16T06: 55: 30.071459 # 5355] INFO-: Cannot render console from 61.215.151.83! Allowed networks: 127.0.0.1, :: 1, 127.0.0.0 /127.255.255.255
I, [2021-09-16T06: 55: 30.074104 # 5355] INFO-: Processing by PostsController # show as HTML
I, [2021-09-16T06: 55: 30.074818 # 5355] INFO-: Parameters: {"id"= >
"twenty one"}
D, [2021-09-16T06: 55: 30.077288 # 5355] DEBUG-: User Load (0.3ms) SELECT `users`. * FROM` users` WHERE `users`.` id`= 62 LIMIT 1
D, [2021-09-16T06: 55: 30.078057 # 5355] DEBUG-: ↳ app /helpers /sessions_helper.rb: 3D, [2021-09-16T06: 55: 30.080427 # 5355] DEBUG-: Post Load (1.3ms) SELECT `posts`. * FROM` posts` WHERE `posts`.` id`= 21 LIMIT 1D, [2021 -09-16T06: 55: 30.081183 # 5355] DEBUG-: ↳ app /controllers /posts_controller.rb: 11
I, [2021-09-16T06: 55: 30.083915 # 5355] INFO-: Rendering posts /show.html.erb within layouts /application
D, [2021-09-16T06: 55: 30.087049 # 5355] DEBUG-: Comment Load (0.2ms) SELECT `comments`. * FROM` comments` WHERE `comments`.`post_id`= 21
D, [2021-09-16T06: 55: 30.087545 # 5355] DEBUG-: ↳ app /views /posts /show.html.erb: 5
I, [2021-09-16T06: 55: 30.089086 # 5355] INFO-: Rendered posts /show.html.erb within layouts /application (4.4ms)
I, [2021-09-16T06: 55: 30.090207 # 5355] INFO-: Completed 200 OK in 15ms (Views: 7.4ms | ActiveRecord: 1.8ms)
  • Answer # 1

    How about doing something like ↓?

    def show
        @post= Post.find (params [: id])
        @comments= @ post.comments
        @comment= Comment.new
        @comments= Comment.all # Delete this line
      end
    

    If you do that, the comments will not be displayed. Added the log when @comments= Comment.all is deleted.

    tamu2021-09-22 23:02:00

    D in the log, [2021-09-16T06: 55: 29.942189 # 5355] DEBUG-: Unpermitted parameter:: post_id seems to be the cause. Since post_id is not included in params.require (: comment) .permit (: content), it seems that a comment with null post_id is created without post_id when creating a comment. Therefore, there is no comment associated with @ post.comments.

    Shinichi Maeshima2021-09-22 23:02:00
  • Answer # 2

    How about doing something like ↓?

    def show
        @post= Post.find (params [: id])
        @comments= @ post.comments
        @comment= Comment.new
        @comments= Comment.all # Delete this line
      end
    

    If you do that, the comments will not be displayed. Added the log when @comments= Comment.all is deleted.

    tamu2021-09-22 23:02:00

    D in the log, [2021-09-16T06: 55: 29.942189 # 5355] DEBUG-: Unpermitted parameter:: post_id seems to be the cause. Since post_id is not included in params.require (: comment) .permit (: content), it seems that a comment with null post_id is created without post_id when creating a comment. Therefore, there is no comment associated with @ post.comments.

    Shinichi Maeshima2021-09-22 23:02:00