Comments are reflected in all posts.

Asked 1 years ago, Updated 1 years ago, 37 views

I have implemented the comment function of the posted version, but all posts reflect the comments.

As I looked into various things and rewritten the code, I couldn't collect the code, so I came up with a question.
I want you to help me.Thank you for your cooperation.

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"=>"テストk目んと"}, "commit"=>"コメントする", "post_id"=>"22"}
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: "テストk目んと", 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,   [ 20 21 - - 2009 : 33 - 35 Fax 450   # 361 ]   the DEBUG   Fighter Creek   - Nominating   :   Model : (. ms )   IN ` Recommendations from States of ` FLICKR USER TO IN TO     (ColorMode  , and Content ` ` us _ `, ` c, for example Jack.The   _ `, ` update selected from a 1799-1811 `   )  , ALS, FRIDAY   ( , ' kg me cancer tesuto ',   ' 2021 - 22 : 33 : 9th - 15th   ' ,   ' 2021 - 22 : 33 : 9th - 15th   ' ) ')
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><%[email protected]%>/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, 'Comments'%>
    <%=f.hidden_field:post_id,value:@post.id%>
    <%=f.text_area:content%>
    <br>
    <%=f.submit'%>
<%end%>
<%=link_to "Back to Home", posts_path%>

commentcontroller

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
     [email protected] spect 
    [email protected]
     redirect_back(fallback_location:root_path)
    else
      @comments=Comment.all
      redirect_back(fallback_location:root_path)
    end
  end
  
private
  def comment_params
    param.require(:comment).permit(:content)
  end
end

postcontroller (show action excerpt)

def show
    @post=Post.find (params[:id])
    @[email protected]
    @ 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
    values:shopname,presence:true,length:{maximum:20}
    values:shopaddress,presence:true,length:{maximum:30}
    values:shopcontent,presence:true,length:{maximum:300}
    has_many —Comments
end

class Comment <ApplicationRecord
  values:content, presence:true, length:{maximum:200}
  belongs_to:user, optional:true
  belongs_to:post, optional:true
end

Routing

Rails.application.routes.drawdo
  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=Logging when 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 : 9 - 16 6 : 0 : 29 the rules # 5 355 392   ]     - INFO   : Venkat   name :   { " utf8 " = and gt " ✓ "   " yt Tokenizer " Point-to-Point " Vint Cerf, city " = and gt " QH HighScore App X 7 Oct. 4, which Np Business New pj 7G York City 0 X 900 km away from the seven KR Adamovicz, BOD or has been fixed pattern Zm QOL (quality of DT 1 / No 3, Oxyconger G1A includes Q Management / Minuum AMC I 34 class == "   " Recommendations from we can. " and gt { " Status information _ " = and gt " 2 1 "   " contentDispositionoption we can. " = & Post ; " tesuto komento " } ,   " com " Comments " = and gt " And it's hard to find fault "   " Status information _ " = and gt " 2 1 "}
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: "テストコメント", 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 : - 16 6 : 0 : 9, 9, 44 724 350 355 # 5 ]   the DEBUG   Fighter Creek   - Nominating   :   Model : (. 2 ms )   IN ` Recommendations from States of ` FLICKR USER TO IN TO     (ColorMode  , and Content ` ` us _ `, ` c, for example Jack.The   _ `, ` update selected from a 1799-1811 `   )  , ALS, FRIDAY   ( , ' strike ' テ (  komento ',   ' 20 21 - 109 - 1 6 : June 5 : ' ,   ' 20 21 - 109 - 1 6 : June 5 : ' )
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-1606: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.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"=>"21"}
D, [2021-09-16T06:55:30.077288#5355] DEBUG --- User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id`=62 LIMIT1
D, [2021-09-16T06:55:30.078057#5355] DEBUG --- appapp/helpers/sessions_helper.rb:3
D, [2021-09-16T06:55:30.080427#5355] DEBUG --- Post Load (1.3ms) SELECT`posts`.* FROM`posts`WHERE`posts`.`id`=21 LIMIT1
D, [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 with in 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 --- appapp/views/posts/show.html.erb:5
I, [2021-09-16T06:55:30.089086#5355] INFO --- Rendered posts/show.html.erb with in layouts/application (4.4ms)
I, [2021-09-16T06:55:30.090207#5355] INFO --—Completed 200 OK in 15 ms (Views: 7.4 ms | ActiveRecord: 1.8 ms)

ruby-on-rails ruby mysql

2022-09-30 17:03

1 Answers

How about ↓?

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


2022-09-30 17:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.