掲示版のコメント機能を実装しています
投稿をするとCouldn't find Post without an IDが出てしまい先に進みません。助けてください
コメント投稿ページ(posts.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.text_area :content %> <br> <%= f.submit 'コメントする' %> <% end %> <%= link_to "ホームへ戻る", posts_path %>
commentconrtroller
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_to post_comments_path else @comments = Comment.all redirect_back(fallback_location: root_path) end end private def comment_params params.require(:comment).permit(:content) end end
postscontoroller(showアクション抜粋)
def show @post = Post.find(params[:id]) @comments = @post.comments @comment = Comment.new @comments = Comment.all 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] get "comments", to: "posts#show" end get "search", to: "posts#search" end
各モデル
Postモデル 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 Commentモデル class Comment < ApplicationRecord validates :content, presence: true, length: { maximum: 200 } belongs_to :user, optional: true belongs_to :post, optional: true end Userモデル class User < ApplicationRecord has_secure_password has_many :posts has_many :comments end
エラーログ(コメント投稿時)
I, [2021-09-14T22:41:34.937662 #3380] INFO -- : Started POST "/posts/20/comments" for 118.103.63.156 at 2021-09-14 22:41:34 +0000 I, [2021-09-14T22:41:34.938666 #3380] INFO -- : Cannot render console from 118.103.63.156! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 D, [2021-09-14T22:41:34.940140 #3380] DEBUG -- : (0.3ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483 D, [2021-09-14T22:41:34.940885 #3380] DEBUG -- : ↳ /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.6/lib/active_record/log_subscriber.rb:98 I, [2021-09-14T22:41:34.942503 #3380] INFO -- : Processing by CommentsController#create as HTML I, [2021-09-14T22:41:34.943161 #3380] INFO -- : Parameters: {"utf8"=>"✓", "authenticity_token"=>"F70fPaNTzBWNSKLwhqbm9ZOuMLWQmkmy/MeEFy2Are3VMwjCTKv1o/Z+NHDrTaHkg4J0BS/VDZRDzcWYL9VTdQ==", "comment"=>{"content"=>"テストコメントteratail"}, "commit"=>"コメントする", "post_id"=>"20"} D, [2021-09-14T22:41:34.945111 #3380] DEBUG -- : User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 47 LIMIT 1 D, [2021-09-14T22:41:34.945874 #3380] DEBUG -- : ↳ app/helpers/sessions_helper.rb:3 D, [2021-09-14T22:41:34.946926 #3380] DEBUG -- : #<ActiveModel::Errors:0x0000559a87356908 @base=#<Comment id: nil, content: "テストコメントteratail", user_id: 47, post_id: nil, created_at: nil, updated_at: nil>, @messages={}, @details={}> D, [2021-09-14T22:41:34.947934 #3380] DEBUG -- : (0.1ms) BEGIN D, [2021-09-14T22:41:34.948733 #3380] DEBUG -- : ↳ app/controllers/comments_controller.rb:11 D, [2021-09-14T22:41:34.950549 #3380] DEBUG -- : Comment Create (0.3ms) INSERT INTO `comments` (`content`, `user_id`, `created_at`, `updated_at`) VALUES ('テ ストコメントteratail', 47, '2021-09-14 22:41:34', '2021-09-14 22:41:34') D, [2021-09-14T22:41:34.951253 #3380] DEBUG -- : ↳ app/controllers/comments_controller.rb:11 D, [2021-09-14T22:41:34.955208 #3380] DEBUG -- : (3.2ms) COMMIT D, [2021-09-14T22:41:34.955776 #3380] DEBUG -- : ↳ app/controllers/comments_controller.rb:11 I, [2021-09-14T22:41:34.956586 #3380] INFO -- : Redirected to https://ac7c34ad0f0d4d6c88e67a1fd746ee4a.vfs.cloud9.ap-northeast-1.amazonaws.com/posts/20/comments I, [2021-09-14T22:41:34.957182 #3380] INFO -- : Completed 302 Found in 13ms (ActiveRecord: 4.0ms) I, [2021-09-14T22:41:35.107756 #3380] INFO -- : Started GET "/posts/20/comments" for 118.103.63.156 at 2021-09-14 22:41:35 +0000 I, [2021-09-14T22:41:35.108673 #3380] INFO -- : Cannot render console from 118.103.63.156! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 I, [2021-09-14T22:41:35.110139 #3380] INFO -- : Processing by PostsController#show as HTML I, [2021-09-14T22:41:35.110760 #3380] INFO -- : Parameters: {"post_id"=>"20"} D, [2021-09-14T22:41:35.113932 #3380] DEBUG -- : User Load (0.6ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 47 LIMIT 1 D, [2021-09-14T22:41:35.114665 #3380] DEBUG -- : ↳ app/helpers/sessions_helper.rb:3 I, [2021-09-14T22:41:35.115834 #3380] INFO -- : Completed 404 Not Found in 3ms (ActiveRecord: 0.6ms) F, [2021-09-14T22:41:35.117444 #3380] FATAL -- : F, [2021-09-14T22:41:35.118056 #3380] FATAL -- : ActiveRecord::RecordNotFound (Couldn't find Post without an ID): F, [2021-09-14T22:41:35.118692 #3380] FATAL -- : F, [2021-09-14T22:41:35.119067 #3380] FATAL -- : app/controllers/posts_controller.rb:11:in `show' I, [2021-09-14T22:41:35.152258 #3380] INFO -- : Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout I, [2021-09-14T22:41:35.156247 #3380] INFO -- : Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb I, [2021-09-14T22:41:35.158642 #3380] INFO -- : Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.6/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (2.1ms) I, [2021-09-14T22:41:35.161905 #3380] INFO -- : Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb I, [2021-09-14T22:41:35.163261 #3380] INFO -- : Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.0ms) I, [2021-09-14T22:41:35.167385 #3380] INFO -- : Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb I, [2021-09-14T22:41:35.168372 #3380] INFO -- : Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms) I, [2021-09-14T22:41:35.168738 #3380] INFO -- : Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (16.0ms)
よろしくお願いします
あなたの回答
tips
プレビュー