掲示版のコメント機能を実装しているのですが、全ての投稿にコメントが反映されてしまいます。

色々調べていきコードを書き換えていくうちにコードが収集つかなかくなり、質問に至りました。  
皆さんの力を貸して欲しいです。よろしくお願いします。

* * *

ログ

```

    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, [2021-09-15T22:33:35.079450 #8361] DEBUG -- :   Comment Create (0.8ms)  INSERT INTO `comments` (`content`, `user_id`, `created_at`, `updated_at`) VALUES ('テストk目んと', 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)
    
```

コメント投稿画面（show.html.erb)

```

    <h1>投稿詳細ページ</h1>
    <h3><%= @post.shopcontent %></h3>
    
    <h2>コメント一覧</h2>
    <% @comments.each do |c| %>
      <div>
        
        <%= c.content %>
        <hr>
      </div>
    <% end %>
    
    <%= form_with model: [@post,@comment] do |f| %>
        <%= f.label :content, 'コメント内容' %>
        <%= f.hidden_field :post_id, value: @post.id %>
        <%= f.text_area :content %>
        <br>
        <%= f.submit 'コメントする' %>
    <% end %>
    <%= link_to "ホームへ戻る", 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
         logger.debug @comment.errors.inspect 
        if @comment.save
         redirect_back(fallback_location: root_path)
        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アクション抜粋）

```

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

各モデル（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
    
```

ルーティング

```

    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 = 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"=>"テストコメント"}, "commit"=>"コメントする", "post_id"=>"21"}
    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-09-16T06:55:29.947243 #5355] DEBUG -- :   Comment Create (0.2ms)  INSERT INTO `comments` (`content`, `user_id`, `created_at`, `updated_at`) VALUES ('テ ストコメント', 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"=>"21"}
    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:3
    D, [2021-09-16T06:55:30.080427 #5355] DEBUG -- :   Post Load (1.3ms)  SELECT  `posts`.* FROM `posts` WHERE `posts`.`id` = 21 LIMIT 1
    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 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)
    
```
CC BY-SA 4.0
2021-09-15T22:48:33.903
82518
2021-09-16T06:57:25.197
2021-09-16T06:57:25.197
47497
47497
question
ruby-on-rails ruby mysql
コメントが全ての投稿に反映されてしまいます
↓のようにするとどうでしょうか？

```

     def show
        @post = Post.find(params[:id])
        @comments = @post.comments
        @comment = Comment.new
        @comments = Comment.all # この行を消す
      end
    
```
CC BY-SA 4.0
2021-09-16T01:13:00.240
82523
2021-09-16T01:13:00.240
205
82518
answer